[Flashcoders] Declaring Function Variables
What's the best practice for declaring reused variables within a function in AS3? For example, if you use count several times in your function, if you declare it at the start of your function, var count:Number; it makes it harder to cut and paste your code for use elsewhere but you get errors if redeclare it (like you could in AS2) throughout your script. David Benman Interactive Developer d...@dbenman.com http://www.dbenman.com (508) 954-1202 (cell) (315) 637-8487 (home office) ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Declaring Function Variables
Declaring it within a function will make the variable LOCAL (= usage in that function only). You only declare it outside, once and (re)use it as often as you like. HTH Cor -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Benman Sent: vrijdag 8 januari 2010 12:16 To: Flash Coders List Subject: [Flashcoders] Declaring Function Variables What's the best practice for declaring reused variables within a function in AS3? For example, if you use count several times in your function, if you declare it at the start of your function, var count:Number; it makes it harder to cut and paste your code for use elsewhere but you get errors if redeclare it (like you could in AS2) throughout your script. David Benman Interactive Developer d...@dbenman.com http://www.dbenman.com (508) 954-1202 (cell) (315) 637-8487 (home office) ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.725 / Virus Database: 270.14.129/2605 - Release Date: 01/07/10 08:35:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Declaring Function Variables
David Benman wrote: What's the best practice for declaring reused variables within a function in AS3? For example, if you use count several times in your function, if you declare it at the start of your function, var count:Number; it makes it harder to cut and paste your code for use elsewhere but you get errors if redeclare it (like you could in AS2) throughout your script. It's better towards the start, though I often declare them inline in loops (slapped wrist, probably). Since counts are whole numbers, it would really be better to use int or uint.. Paul David Benman Interactive Developer d...@dbenman.com http://www.dbenman.com (508) 954-1202 (cell) (315) 637-8487 (home office) ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Declaring Function Variables
Cor wrote: Declaring it within a function will make the variable LOCAL (= usage in that function only). You only declare it outside, once and (re)use it as often as you like. I would advise that it's best to avoid doing that. Most of the time it won't give you a problem, but then you'll spend some time trying to find out why that function call is messing up the loop count of the code it's called in. HTH Cor -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Benman Sent: vrijdag 8 januari 2010 12:16 To: Flash Coders List Subject: [Flashcoders] Declaring Function Variables What's the best practice for declaring reused variables within a function in AS3? For example, if you use count several times in your function, if you declare it at the start of your function, var count:Number; it makes it harder to cut and paste your code for use elsewhere but you get errors if redeclare it (like you could in AS2) throughout your script. David Benman Interactive Developer d...@dbenman.com http://www.dbenman.com (508) 954-1202 (cell) (315) 637-8487 (home office) ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.725 / Virus Database: 270.14.129/2605 - Release Date: 01/07/10 08:35:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Declaring Function Variables
Mmmm, I have never experienced any problems with it. -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Paul Andrews Sent: vrijdag 8 januari 2010 12:47 To: Flash Coders List Subject: Re: [Flashcoders] Declaring Function Variables Cor wrote: Declaring it within a function will make the variable LOCAL (= usage in that function only). You only declare it outside, once and (re)use it as often as you like. I would advise that it's best to avoid doing that. Most of the time it won't give you a problem, but then you'll spend some time trying to find out why that function call is messing up the loop count of the code it's called in. HTH Cor -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Benman Sent: vrijdag 8 januari 2010 12:16 To: Flash Coders List Subject: [Flashcoders] Declaring Function Variables What's the best practice for declaring reused variables within a function in AS3? For example, if you use count several times in your function, if you declare it at the start of your function, var count:Number; it makes it harder to cut and paste your code for use elsewhere but you get errors if redeclare it (like you could in AS2) throughout your script. David Benman Interactive Developer d...@dbenman.com http://www.dbenman.com (508) 954-1202 (cell) (315) 637-8487 (home office) ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.725 / Virus Database: 270.14.129/2605 - Release Date: 01/07/10 08:35:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.725 / Virus Database: 270.14.129/2605 - Release Date: 01/07/10 08:35:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Declaring Function Variables
Cor wrote: Mmmm, I have never experienced any problems with it. It just means you havent used the same variable in a loop with nested functions that are also using the variable. -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Paul Andrews Sent: vrijdag 8 januari 2010 12:47 To: Flash Coders List Subject: Re: [Flashcoders] Declaring Function Variables Cor wrote: Declaring it within a function will make the variable LOCAL (= usage in that function only). You only declare it outside, once and (re)use it as often as you like. I would advise that it's best to avoid doing that. Most of the time it won't give you a problem, but then you'll spend some time trying to find out why that function call is messing up the loop count of the code it's called in. HTH Cor -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Benman Sent: vrijdag 8 januari 2010 12:16 To: Flash Coders List Subject: [Flashcoders] Declaring Function Variables What's the best practice for declaring reused variables within a function in AS3? For example, if you use count several times in your function, if you declare it at the start of your function, var count:Number; it makes it harder to cut and paste your code for use elsewhere but you get errors if redeclare it (like you could in AS2) throughout your script. David Benman Interactive Developer d...@dbenman.com http://www.dbenman.com (508) 954-1202 (cell) (315) 637-8487 (home office) ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.725 / Virus Database: 270.14.129/2605 - Release Date: 01/07/10 08:35:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.725 / Virus Database: 270.14.129/2605 - Release Date: 01/07/10 08:35:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Declaring Function Variables
OOOh, indeed, that would be a bad choice. -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Paul Andrews Sent: vrijdag 8 januari 2010 13:16 To: Flash Coders List Subject: Re: [Flashcoders] Declaring Function Variables Cor wrote: Mmmm, I have never experienced any problems with it. It just means you havent used the same variable in a loop with nested functions that are also using the variable. -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Paul Andrews Sent: vrijdag 8 januari 2010 12:47 To: Flash Coders List Subject: Re: [Flashcoders] Declaring Function Variables Cor wrote: Declaring it within a function will make the variable LOCAL (= usage in that function only). You only declare it outside, once and (re)use it as often as you like. I would advise that it's best to avoid doing that. Most of the time it won't give you a problem, but then you'll spend some time trying to find out why that function call is messing up the loop count of the code it's called in. HTH Cor -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Benman Sent: vrijdag 8 januari 2010 12:16 To: Flash Coders List Subject: [Flashcoders] Declaring Function Variables What's the best practice for declaring reused variables within a function in AS3? For example, if you use count several times in your function, if you declare it at the start of your function, var count:Number; it makes it harder to cut and paste your code for use elsewhere but you get errors if redeclare it (like you could in AS2) throughout your script. David Benman Interactive Developer d...@dbenman.com http://www.dbenman.com (508) 954-1202 (cell) (315) 637-8487 (home office) ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.725 / Virus Database: 270.14.129/2605 - Release Date: 01/07/10 08:35:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.725 / Virus Database: 270.14.129/2605 - Release Date: 01/07/10 08:35:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.725 / Virus Database: 270.14.129/2605 - Release Date: 01/07/10 08:35:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
[Flashcoders] Declaring Function Variables
...Or if you like to perform a recursion (particular case of nesting). I do not belive there is a good way to re-declare a variable inside the scope of one function. (?) You could comment out vars, copy/paste them with active code and uncomment when needed; it is more a text editor issue: //var x:Number; //var y:Number; //var a:Number; a = x + y; I think that splitting long function bodies into separate short ones is a better idea. All local variables may be declared (and not re-declared) inside these short functions: var x = calcA( calB() + calcC() ) / calcD(); g Friday, January 08, 2010 (1:18:45 PM) Cor wrote: OOOh, indeed, that would be a bad choice. -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Paul Andrews Sent: vrijdag 8 januari 2010 13:16 To: Flash Coders List Subject: Re: [Flashcoders] Declaring Function Variables Cor wrote: Mmmm, I have never experienced any problems with it. It just means you havent used the same variable in a loop with nested functions that are also using the variable. -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Paul Andrews Sent: vrijdag 8 januari 2010 12:47 To: Flash Coders List Subject: Re: [Flashcoders] Declaring Function Variables Cor wrote: Declaring it within a function will make the variable LOCAL (= usage in that function only). You only declare it outside, once and (re)use it as often as you like. I would advise that it's best to avoid doing that. Most of the time it won't give you a problem, but then you'll spend some time trying to find out why that function call is messing up the loop count of the code it's called in. HTH Cor -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Benman Sent: vrijdag 8 januari 2010 12:16 To: Flash Coders List Subject: [Flashcoders] Declaring Function Variables What's the best practice for declaring reused variables within a function in AS3? For example, if you use count several times in your function, if you declare it at the start of your function, var count:Number; it makes it harder to cut and paste your code for use elsewhere but you get errors if redeclare it (like you could in AS2) throughout your script. David Benman Interactive Developer d...@dbenman.com http://www.dbenman.com (508) 954-1202 (cell) (315) 637-8487 (home office) ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.725 / Virus Database: 270.14.129/2605 - Release Date: 01/07/10 08:35:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.725 / Virus Database: 270.14.129/2605 - Release Date: 01/07/10 08:35:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.725 / Virus Database: 270.14.129/2605 - Release Date: 01/07/10 08:35:00 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Declaring Function Variables
The rule I follow (and one that I think is best for code readability) is that you declare a variable the first time you use it. If you're using it in multiple loops, you declare it above the first loop that uses it. This is a trivial example: private function foo(data:Array):void { var myVar:Boolean = true; var i:int; var len:int = data.length; for (i = 0; i len; ++i) { if (data[i] == ) { myVar = false; break; } } if (myVar) { for (i = 0; i len; ++i) { trace(data[i].length); } } } If you're copying and pasting those loops around, you really should consider learning how to write DRY code and use subroutines. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders