Re: [Flashcoders] Re: Newbie AS3 question

2005-11-01 Thread Spike
Yes indeed!

You can usually find an alternative approach to solving the problem than
using the singleton pattern, but it is distinctly different to using static
methods.

Glad to hear it works the way it does in other languages at least.

Spike

On 10/29/05, A.Cicak <[EMAIL PROTECTED]> wrote:
>
> Problem with JasterXL's class is initialization. For example if you had to
> initialize something in that class you should make static method called
> InitCurrencyFormatter(). But when to call it? If other classes are using
> CurrencyFormatter than you should ensure to call InitCurrencyFormatter()
> before construction of any class which uses it is called. But what if
> InitCurrencyFormatter also depends on some other class which is made just
> with static methods (like JasterXL's). In that case that other class would
> have to have InitOtherClass() and it also should be called before other
> objects are constructed and before InitCurrencyFormatter. Now imagine 20
> classes like that and thousands of lines of code depending on them, it
> would
> be almost impossible to keep track when to initialize any of these
> classes,
> and it would be very error prone (you change order of execution and you
> start using uninitialized objects). If you make these classes singletons
> you
> don't have that problem, because when you call getInstance() if object was
> not initialized getInstance will call new and that will call its
> constructor
> ( which now replaces InitCurrencyFormatter, InitOtherClass, etc..) and it
> will ensure that all objects are initialized at time of their use, and you
> do not have to worry about order of execution.
>
>
> "Spike" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> Sure,
>
> Here's a slightly more complete implementation of that last example:
>
>
> public class CurrencyFormatter {
>
> private static formatter:CurrencyFormatter;
>
> public function getInstance():CurrencyFormatter {
> // use ExternalInterface or IP lookup or whatever to determine locale
> if (locale == "UK") {
> formatter = new UKCurrencyFormatter();
> } else {
> formatter = new USCurrencyFormatter();
> }
> return formatter;
> }
>
> class USCurrencyFormatter extends CurrencyFormatter {
>
> public formatValue(val:Number) {
> // very simplistic formatting
> return "$" + String(val);
> }
> }
>
> class UKCurrencyFormatter extends CurrencyFormatter {
>
> public formatValue(val:Number) {
> // very simplistic formatting
> return "£" + String(val);
> }
> }
>
> }
>
> Let me know if that explains it a bit better.
>
> Spike
>
>
> On 10/29/05, JesterXL <[EMAIL PROTECTED]> wrote:
> >
> > Can you elaborate? Why wouldn't the static class work in that case?
> >
> > - Original Message -
> > From: "Spike" <[EMAIL PROTECTED]>
> > To: "Flashcoders mailing list" 
> > Sent: Friday, October 28, 2005 9:54 PM
> > Subject: Re: [Flashcoders] Newbie AS3 question
> >
> >
> > ok,
> >
> > That's just a static class.
> >
> > Like I said, there's a subtle but important difference between singleton
> > and
> > a static class.
> >
> > Here's another example.
> >
> > You have a requirement to provide a currency formatter.
> >
> > One way to do this is to create a singleton that returns a different
> > currency formatter depending on which locale you are in.
> >
> > So in the class you would have something like this (Omitting method
> > declarations for simplicty):
> >
> > public class CurrencyFormatter {
> >
> > class USCurrencyFormatter extends CurrencyFormatter {
> >
> > }
> >
> >
> > class UKCurrencyFormatter extends CurrencyFormatter {
> >
> > }
> > }
> >
> > Now if I call CurrencyFormatter.getInstance() it gives me the correct
> > formatter for my locale.
> >
> > With your static class approach you have to check in every method what
> the
> > locale is and handle it accordingly. That's fine for one or two locales,
> > but
> > if you want to handle 20, it gets pretty ugly.
> >
> > You can solve the problem in other ways of course, but it does
> demonstrate
> > the difference between static classes and the singleton pattern. The
> > singleton pattern offers you a lot more possibilities.
> >
> > Spike
> >
> > On 10/29/05, JesterXL <[EMAIL PROTECTED]> wrote:
> > >
> > > To clarify:
> > >
> > >
> > > class ServerConnection
> > > {
> > > private static var url;
> > > private static var port;
> > > private static var socket;
> > >
> > > public static function connect(p_url, p_port)
> > > {
> > > url = p_url;
> > > port = p_port;
> > > socket = new Socket();
> > > socket.connect(url, port);
> > > }
> > >
> > > public static function getData()
> > > {
> > > // Simple function that gets something from the server.
> > > }
> > > }
> > >
> > > Then to use:
> > >
> > > ServerConnection.connect(myURL, myPort);
> > >
> > > - Original Message -
> > > From: "JesterXL" <[EMAIL PROTECTED]>
> > > To: "Flashcoders mailing list" 
> > > Sent: Friday, October 28, 2005 9:25 PM
> > > Subject: Re: [Flashcoders] Newbie AS3 question
> > >
> > >
> > > Naw

Re: [Flashcoders] Re: Newbie AS3 question

2005-10-30 Thread Cedric Muller
I learned programming in Flash and I use this everyday, almost 
everyline ;)

because scope has always been one of the thoughest thing in Flash :-)

* cedric thanks ryanm


 In my opinion (and in the opinion of many much more competent 
developers than myself), it is always good to be explicit about scope, 
because it removes any ambiguity from the code, drastically reducing 
the possibility of expensive and time consuming breakage during 
maintenance.


   One more thing worth mentioning, and I don't say this to be rude or 
as a slight to anyone on this list, but the opinion that the this 
reference is bad and should be avoided seems to be unilaterally coming 
from people who learned programming in Flash, while the opinion that 
scope should be stated explicitly seems to be coming from people with 
more formal training in software development and experience in 
(non-Flash) real world development. Personally, I have to give more 
weight to the opinions of people with more formal training and more 
varied real-world experience, because there are some things you just 
can't learn in a year or two of using Flash for web development. There 
is a reason that experienced developers like to type those extra 5 
characters, and it is to save time, money, and the embarrasment of 
explaining to your boss that you deleted something without fully 
understanding the implications.


ryanm
___
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] Re: Newbie AS3 question

2005-10-29 Thread Scott Hyndman
"this" can also be used to refer an instance's member variable explicitly. 
Since scoping rules allow for a local variable (in a method) to be named the 
same as an instance member variable, "this" is required to differentiate 
between the two.
 
(Sorry about the html mail)
 
Scott

-Original Message- 
From: [EMAIL PROTECTED] on behalf of A.Cicak 
Sent: Fri 28/10/2005 6:32 PM 
To: flashcoders@chattyfig.figleaf.com 
Cc: 
Subject: [Flashcoders] Re: Newbie AS3 question



Well, I dont agree, "this" keyword refers to current class, so its only 
more
typing to include it, and making code less
readable. Only reason keyword "this" exists is if you want to pass 
reference
to current object somewhere, in which case
you must use "this". To me using "this" in your code makes you look like
wannabe-programmer, :) But I gues its matter of taste. btw, old VB does 
not
have "this" keyword, and if you were reffering to VB(.NET), it is more 
OOP
and more complex than AS3, so I gues programmers in VB.NET (if there are
some, since C# is there) are not wannabe-programmers :)


"ryanm" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>> What I don't get is why it needs "this.addChild" instead of just
>> addChild. I've been sick of the keyword "this" for a long time and 
have
>> since avoided it in AS2.
>>
>> Any reason that it needs to be back in for AS3?
>>
>Maybe because it's one of the most useful scope references ever
> invented?
>
>The fundamental concept that you seem to miss is that "addChild" is
> meaningless by itself, it is a method of an object (in proper OOP
> development), and if you just say "addChild", who is adding the 
child? You
> need a reference. You could do it like this if you like:
>
> class Game extends MovieClip {
>var world:MovieClip;
>var bg:MovieClip;
>function Game(){
>var GameReference:Game = this;
>world = new MovieClip();
>
>GameReference.addChild( world );
>
>bg = new MovieClip();
>world.addChild( bg );
>}
> }
>
>The point is, you shouldn't use functions that aren't attached to
> objects, it's bad form, and it's thoroughlly confusing to people who 
later
> have to maintain your code. Besides, it makes you look like one of 
those
> wannabe-programmer VB guys. ;-)
>
> ryanm
> ___
> 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


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Re: Newbie AS3 question

2005-10-29 Thread Robert Tweed

ryanm wrote:

> You do know that it (this.) is being added for you at compile time in
AS2, right?

Who cares? All that means is that there is no semantic difference 
between the two.


   Not quite. What it means is that the "this" is assumed, which is not 
always what you want.


You can force class scope by including "this", but there's no way to do 
the opposite. Local (or class) scope always obscures global scope, 
that's just a fact of life. The only exception is built-in function like 
trace and eval, which normally obscure everything else. That's probably 
part of the reason AS3 is more heavily based around namespaces - you can 
more clearly state what scope you want if everything resides in a unique 
namespace. That takes us back to the point about whether you should 
include "this" everywhere - doing so is akin to including the fully 
qualified name of every class instead of just importing the package. 
Which is easier to read?


- Robert
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Re: Newbie AS3 question

2005-10-29 Thread ryanm
Of course there exists edge cases where that isnt feasible, but most 
programs dont implement DES algorithms (to relate this to an earlier post) 
and a lot of legacy code i have worked with has benefitted from being 
re-factored.


   I actually do have classes with methods so large that I had to make a 
base class containing only that method and then extend it (because of the 
size limit imposed by Flash), but as was mentioned, they are mostly 
encryption or compression classes, and are special cases.


ryanm 


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Re: Newbie AS3 question

2005-10-29 Thread ryanm
All that to say, if your going to be putting "this" in front of every 
class
member in AS2 and in AS3 you'll be missing the neat advantage of 
simplicity.


   As it were, the classes I write are rarely self-referential. Properties 
such as position, visibility, etc, are usually handled elsewhere or by other 
means rather than directly referring to itself, so you won't find "this" in 
my code a lot either. When it does appear it is the rare exception, not the 
rule, but by the same token, you also won't find referenceless method calls 
either. I very, very rarely put something like "this._visible=false;" in a 
constructor, and usually only because it's a quickie, one-off project that 
won't need to be maintained, and even if maintenance is required, there is 
usually only one class to deal with and everything is right there. But when 
I do something like that, I always use "this", just so that it's clear what 
I'm referring to.


   You also rarely find _parent references in my code, because usually when 
I need a reference to the parent, I pass that reference in at creation time 
and store it as a member variable in the class. If it's not important enough 
to be a member of the class, then it probably isn't necessary, and if it's 
necessary, it's important enough to be a member. So really, "this" is the 
only


ryanm 


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] Re: Newbie AS3 question

2005-10-29 Thread Frédéric v . Bochmann
Hey ryanm, 

Imagine this:

class MyClass {
 private var myName:String;
 public function MyClass(myName:String) {
   this.myName = myName
 }
}

Or:
class MyClass {
 public function MyClass(myEventDispatcher) {
   var myListener = new Object();
   myListener.controler = this;
   myListener.click = function (eventObj) {
 this.controler.onMyClick(eventObj);
   }
   myEventDispatcher.addEventListener("click", myListener);
   
 }
 public function onMyClick (eventObj) {
   
 }
}

Naturally you can use the Delegate object instead of creating an anonymous
function, but the fact states that those are moments you absolutely need to
use "this" and good practice is about using them when you need them / When
you want to write them everywhere, it's really the programmers choice to
write it as he wishes but he should think of future programmers that might
go into that code, they might not want to scroll horizontally that much to
read the code, it just makes lines shorter and easier to read, if well
written.

Now you're saying that the compiler adds "this" everywhere or almost... Well
this is more like the pre-compiler's job, and this is why it actually
exists. The idea is to write human readable ECMA script that isn't too
cluttered, that the pre-compiler can arrange for the compiler to compile. If
we didn't have the pre-compiler we'd be writing a lot of code with a lot of
"this" everywhere and we wouldn't be able to forget to put a semi-column at
the end of each line :P  (a bit like PHP)


On the other side, in AS1 when creating your classes it is about 100%
necessary to specify "this" in front of instance properties or methods.
Things have changed in AS2 and their closer to what standard OOP programming
should look like. If you look in the world of JAVA, you won't find extensive
usage of the "this" statement like you seem to be saying. But it is really
the programmer's discretion to use "this" as he wishes when he doesn't HAVE
to use it; nobody should blame someone for using "this" anytime except if it
does something that is not meant to be.


When programmers modify code, removing the "this" statements everywhere when
going threw somebody's code like your saying, they should be aware of what
this might involve, and it's not the fault of the programmer that put all
those "this" statements, it's the fault of the one that removed them I'd
say. Common sense! So I wouldn't really be annoyed by reading your code that
has "this", everywhere it can be used at. It's ok and so be it.

All that to say, if your going to be putting "this" in front of every class
member in AS2 and in AS3 you'll be missing the neat advantage of simplicity.
I think it's a question of trusting how Flash will get compiled and how it
will run it! Trust ECMA script and know what you're coding, if it's AS1, use
lots of "this"; if your in pure AS2, lighten the number of characters in
your file if you may!:) Let's not even mention AS3, since it's even more a
question of cleaning up your code and making it human readable and OO. Using
"this" everywhere would ruin the fun of writing AS2 and AS3.

And yes there are cases in AS2 where your class method might need "this" in
front of all methods and members it uses; this is mostly the case if you're
redirecting methods to other classes with their prototype, thus returning to
the world of AS1.

Cheers,
Fredz./

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of ryanm
Sent: October 29, 2005 3:39 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Re: Newbie AS3 question

> The idea is to use it (this) when you have two variables with the same
> name, usually to distinguish between the class member and a locally
> declared variable. You must know that :) The compiler won't know to
> use "this" or not in those cases, so it is important to use it in those
> contexts.
>
Actually, the compiler *does* know, it always adds "this." to references

without explicitly stated scope.

Which brings us full circle, all the way back to maintainability. Now, 
if you have a class, and all of the member functions and variables are 
referenceless (using "assumed" scope without the this reference), and all of

a sudden, in the middle of a method, you have a this reference because of a 
potential scope conflict, what does a developer looking at your code 2 years

after you've left the job think about it? Does he remove the single this 
reference, to make it more readable and consistent, thus breaking the method

and blowing up the whole project, all without knowing how or why? Martin 
piped up with "I would refactor the method to make it more readable..

Re: [Flashcoders] Re: Newbie AS3 question

2005-10-29 Thread Martin Wood



ryanm wrote:

The idea is to use it (this) when you have two variables with the same
name, usually to distinguish between the class member and a locally
declared variable. You must know that :) The compiler won't know to
use "this" or not in those cases, so it is important to use it in those
contexts.

   Actually, the compiler *does* know, it always adds "this." to 
references without explicitly stated scope.


   Which brings us full circle, all the way back to maintainability. 
Now, if you have a class, and all of the member functions and variables 
are referenceless (using "assumed" scope without the this reference), 
and all of a sudden, in the middle of a method, you have a this 
reference because of a potential scope conflict, what does a developer 
looking at your code 2 years after you've left the job think about it? 
Does he remove the single this reference, to make it more readable and 
consistent, thus breaking the method and blowing up the whole project, 
all without knowing how or why? Martin piped up with "I would refactor 
the method to make it more readable..." immediately, which, in this 
case, would've broken the method (if he removed the reference) and 
possibly the whole class, which is exactly how these sort of expensive, 
time consuming, and difficult to troubleshoot problems come up. On the 
other hand, if the original developer used the this reference every 
time, it would already be consistent, readable, and explicit about scope.


   The other question is, would Martin (not to pick on Martin, but he 
was vocal about it, so I use him as an example) take a class that stated 
scope explicitly and remove all the this references, possibly breaking 
the class in the process? It sounds like it, from his post. The question 
is, is that a good practice or a bad practice, and does that make him a 
"good" developer, or a "bad" one, given the potential for expensive and 
time consuming breakage caused solely by his dislike for the keyword this?


Its ok, you can pick on me, i dont mind ;)

I'll take each piece seperately.

1. Removing the single 'this' reference.

well no, i wouldnt remove it because i know why its there. If the method 
was long and could / should be refactored, then that is what i would do. 
Of course there exists edge cases where that isnt feasible, but most 
programs dont implement DES algorithms (to relate this to an earlier 
post) and a lot of legacy code i have worked with has benefitted from 
being re-factored.


If i see one 'this' reference among a series of unspecified local 
references then I would look at it carefully and think that the 
programmer had good cause to specify the scope in that case.


One 'this' reference doesnt have much of an impact on readability but 
using it everywhere does, as Jesse highlighted.


I would rarely advocate becoming dogmatic about any technique, *always* 
do this or *always* do that, because its not often an all or nothing 
decision.


Of course, in some far off dreamland this code would also have unit 
tests so that if i did refactor it or unthinkingly removed the one 
reference to 'this' then a nice little red bar would tell me id done 
something bad, but again, im realistic, most code doesnt have unit tests. :)


2. Removing all references to 'this' in supplied code.

It really depends on the level of maintenance im expected to undertake 
with the legacy code. If it was just a minor bugfix or two, or some 
tweak then i would be inclined to save myself the effort and just do 
what is required (i.e. leave the extraneous references to 'this' intact).
If the code required some major work then I would remove the references 
to this, except where necessary.


Like i stated in my first post about this subject, sometimes its 
necessary and i know when that is.


Most of the time it isnt and personally i feel that it clutters the code.

   One more thing worth mentioning, and I don't say this to be rude or 
as a slight to anyone on this list, but the opinion that the this 
reference is bad and should be avoided seems to be unilaterally coming 
from people who learned programming in Flash, while the opinion that 
scope should be stated explicitly seems to be coming from people with 
more formal training in software development and experience in 
(non-Flash) real world development. Personally, I have to give more 
weight to the opinions of people with more formal training and more 
varied real-world experience, because there are some things you just 
can't learn in a year or two of using Flash for web development.


Unless you are a mind reader or have access to information about each 
poster that is in this discussion then i would say that its a dangerous 
and false generalization.


I initially learnt my programming way back in the mists of time, before 
flash was but a glint in some guys eye, and have had my fair share of 
formal training and developed in non-flash 'real world situations', but 
in my opinion that isnt really relev

Re: [Flashcoders] Re: Newbie AS3 question

2005-10-29 Thread ryanm

> You do know that it (this.) is being added for you at compile time in
AS2, right?

Who cares? All that means is that there is no semantic difference between 
the two.
   Not quite. What it means is that the "this" is assumed, which is not 
always what you want. And if you need the reference sometimes and other 
times it is extraneous, why not use it all the time for the sake of 
consistency?


ryanm 


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Re: Newbie AS3 question

2005-10-29 Thread ryanm

The idea is to use it (this) when you have two variables with the same
name, usually to distinguish between the class member and a locally
declared variable. You must know that :) The compiler won't know to
use "this" or not in those cases, so it is important to use it in those
contexts.

   Actually, the compiler *does* know, it always adds "this." to references 
without explicitly stated scope.


   Which brings us full circle, all the way back to maintainability. Now, 
if you have a class, and all of the member functions and variables are 
referenceless (using "assumed" scope without the this reference), and all of 
a sudden, in the middle of a method, you have a this reference because of a 
potential scope conflict, what does a developer looking at your code 2 years 
after you've left the job think about it? Does he remove the single this 
reference, to make it more readable and consistent, thus breaking the method 
and blowing up the whole project, all without knowing how or why? Martin 
piped up with "I would refactor the method to make it more readable..." 
immediately, which, in this case, would've broken the method (if he removed 
the reference) and possibly the whole class, which is exactly how these sort 
of expensive, time consuming, and difficult to troubleshoot problems come 
up. On the other hand, if the original developer used the this reference 
every time, it would already be consistent, readable, and explicit about 
scope.


   The other question is, would Martin (not to pick on Martin, but he was 
vocal about it, so I use him as an example) take a class that stated scope 
explicitly and remove all the this references, possibly breaking the class 
in the process? It sounds like it, from his post. The question is, is that a 
good practice or a bad practice, and does that make him a "good" developer, 
or a "bad" one, given the potential for expensive and time consuming 
breakage caused solely by his dislike for the keyword this?


   In my opinion (and in the opinion of many much more competent developers 
than myself), it is always good to be explicit about scope, because it 
removes any ambiguity from the code, drastically reducing the possibility of 
expensive and time consuming breakage during maintenance.


   One more thing worth mentioning, and I don't say this to be rude or as a 
slight to anyone on this list, but the opinion that the this reference is 
bad and should be avoided seems to be unilaterally coming from people who 
learned programming in Flash, while the opinion that scope should be stated 
explicitly seems to be coming from people with more formal training in 
software development and experience in (non-Flash) real world development. 
Personally, I have to give more weight to the opinions of people with more 
formal training and more varied real-world experience, because there are 
some things you just can't learn in a year or two of using Flash for web 
development. There is a reason that experienced developers like to type 
those extra 5 characters, and it is to save time, money, and the 
embarrasment of explaining to your boss that you deleted something without 
fully understanding the implications.


ryanm 


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] Re: Newbie AS3 question

2005-10-29 Thread Frédéric v . Bochmann
Yes and this also applies to AS2. All depending on how you want your
function to work. The usage of variables declared outside a anonymous class
or function is possible but it's important to know in which scope those
variables are being accessed, thus the usage of "this" can be very useful to
know exactly what you are accessing.

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of zwetan
Sent: October 29, 2005 9:03 AM
To: 'Flashcoders mailing list'
Subject: RE: [Flashcoders] Re: Newbie AS3 question

> >>No need to get hyper about "this".
> >>The matter stays that "this" used to be essential in AS1, thus probably
> why
> >>people still like to implicate him in their code. But I agree that
> putting
> >>"this" in an AS2 Class should be used only when necessary.
>  >
>  > You do know that it (this.) is being added for you at compile time in
> AS2, right?
>
> Who cares? All that means is that there is no semantic difference
> between the two.

There is a difference for AS3 ! (and this is the correct behaviour)
http://livedocs.macromedia.com/labs/1/flex/langref/statements.html#this

"To call a function defined in a dynamic class, you must use this to invoke
the function in the proper scope"

zwetan



___
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] Re: Newbie AS3 question

2005-10-29 Thread Frédéric v . Bochmann
The idea is to use it (this) when you have two variables with the same name,
usually to distinguish between the class member and a locally declared
variable. You must know that :) The compiler won't know to use "this" or not
in those cases, so it is important to use it in those contexts.

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Muzak
Sent: October 28, 2005 7:53 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Re: Newbie AS3 question

You do know that it (this.) is being added for you at compile time in AS2,
right?

Muzak

- Original Message - 
From: "Frédéric v. Bochmann" <[EMAIL PROTECTED]>
To: "'Flashcoders mailing list'" 
Sent: Saturday, October 29, 2005 12:53 AM
Subject: RE: [Flashcoders] Re: Newbie AS3 question


> No need to get hyper about "this".
> The matter stays that "this" used to be essential in AS1, thus probably
why
> people still like to implicate him in their code. But I agree that putting
> "this" in an AS2 Class should be used only when necessary.
>
>


___
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] Re: Newbie AS3 question

2005-10-29 Thread Robert Tweed

(-: Tatactic :-) wrote:

In the book of C.Moock, he recommends the redundant use of [this]


No he doesn't. He says "some people prefer" but he also clearly states 
that it is not used anywhere in the book, because it makes code overly 
verbose and hence, less readable. If you want to check, it's on page 87 
(listed in the index under "this (keyword), redundant use of").


That aside, "he told me to do it" isn't a convincing argument anyway.

- Robert
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] Re: Newbie AS3 question

2005-10-29 Thread (-: Tatactic :-)
In the book of C.Moock, he recommends the redundant use of [this]
Sorry, I'm a newbie too but it seems to be a good practice to specify
clearly the scope you refer to.
It's clearly specified in chapter 6 to be aware about the scope.
"Essential ActionScript 2.0 O'Reilly Media, Inc, 2004, ISBN 0-596-00652-7"
Greetings
N

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of zwetan
Sent: samedi 29 octobre 2005 15:03
To: 'Flashcoders mailing list'
Subject: RE: [Flashcoders] Re: Newbie AS3 question

> >>No need to get hyper about "this".
> >>The matter stays that "this" used to be essential in AS1, thus probably
> why
> >>people still like to implicate him in their code. But I agree that
> putting
> >>"this" in an AS2 Class should be used only when necessary.
>  >
>  > You do know that it (this.) is being added for you at compile time in
> AS2, right?
>
> Who cares? All that means is that there is no semantic difference
> between the two.

There is a difference for AS3 ! (and this is the correct behaviour)
http://livedocs.macromedia.com/labs/1/flex/langref/statements.html#this

"To call a function defined in a dynamic class, you must use this to invoke
the function in the proper scope"

zwetan



___
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] Re: Newbie AS3 question

2005-10-29 Thread Robert Tweed

zwetan wrote:

Who cares? All that means is that there is no semantic difference
between the two.


There is a difference for AS3 ! (and this is the correct behaviour)
http://livedocs.macromedia.com/labs/1/flex/langref/statements.html#this

"To call a function defined in a dynamic class, you must use this to invoke
the function in the proper scope"


This holds true for AS2 as well. In fact, this is a perfect example of 
the *right* use of the keyword "this".


In this kind of situation, you need to include "this" to dereference a 
function that is not known to exist at compile time. Otherwise it's an 
ambiguous statement, because the function, not being found in the class 
definition, is sought in the global scope instead. That would be 
especially bad if a similarly-named function does happen to exist in the 
global namespace, because you won't get a compiler error but the wrong 
function would be called.


- Robert
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Re: Newbie AS3 question

2005-10-29 Thread JesterXL
If you utilize a static function, it would be called when the class itself 
is created in ActionScript, before frame 1 even runs:

class Test
{
static var inited = InitThisTest();
}

Why wouldn't that solve it?

- Original Message - 
From: "A.Cicak" <[EMAIL PROTECTED]>
To: 
Sent: Saturday, October 29, 2005 6:59 AM
Subject: [Flashcoders] Re: Newbie AS3 question


Problem with JasterXL's class is initialization. For example if you had to
initialize something in that class you should make static method called
InitCurrencyFormatter(). But when to call it? If other classes are using
CurrencyFormatter than you should ensure to call InitCurrencyFormatter()
before construction of any class which uses it is called. But what if
InitCurrencyFormatter also depends on some other class which is made just
with static methods (like JasterXL's). In that case that other class would
have to have InitOtherClass() and it also should be called before other
objects are constructed and before InitCurrencyFormatter. Now imagine 20
classes like that and thousands of lines of code depending on them, it would
be almost impossible to keep track when to initialize any of these classes,
and it would be very error prone (you change order of execution and you
start using uninitialized objects). If you make these classes singletons you
don't have that problem, because when you call getInstance() if object was
not initialized getInstance will call new and that will call its constructor
( which now replaces  InitCurrencyFormatter, InitOtherClass, etc..) and it
will ensure that all objects are initialized at time of their use, and you
do not have to worry about order of execution.


"Spike" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
Sure,

Here's a slightly more complete implementation of that last example:


public class CurrencyFormatter {

private static formatter:CurrencyFormatter;

public function getInstance():CurrencyFormatter {
// use ExternalInterface or IP lookup or whatever to determine locale
if (locale == "UK") {
formatter = new UKCurrencyFormatter();
} else {
formatter = new USCurrencyFormatter();
}
return formatter;
}

class USCurrencyFormatter extends CurrencyFormatter {

public formatValue(val:Number) {
// very simplistic formatting
return "$" + String(val);
}
}

class UKCurrencyFormatter extends CurrencyFormatter {

public formatValue(val:Number) {
// very simplistic formatting
return "£" + String(val);
}
}

}

Let me know if that explains it a bit better.

Spike


On 10/29/05, JesterXL <[EMAIL PROTECTED]> wrote:
>
> Can you elaborate? Why wouldn't the static class work in that case?
>
> - Original Message -
> From: "Spike" <[EMAIL PROTECTED]>
> To: "Flashcoders mailing list" 
> Sent: Friday, October 28, 2005 9:54 PM
> Subject: Re: [Flashcoders] Newbie AS3 question
>
>
> ok,
>
> That's just a static class.
>
> Like I said, there's a subtle but important difference between singleton
> and
> a static class.
>
> Here's another example.
>
> You have a requirement to provide a currency formatter.
>
> One way to do this is to create a singleton that returns a different
> currency formatter depending on which locale you are in.
>
> So in the class you would have something like this (Omitting method
> declarations for simplicty):
>
> public class CurrencyFormatter {
>
> class USCurrencyFormatter extends CurrencyFormatter {
>
> }
>
>
> class UKCurrencyFormatter extends CurrencyFormatter {
>
> }
> }
>
> Now if I call CurrencyFormatter.getInstance() it gives me the correct
> formatter for my locale.
>
> With your static class approach you have to check in every method what the
> locale is and handle it accordingly. That's fine for one or two locales,
> but
> if you want to handle 20, it gets pretty ugly.
>
> You can solve the problem in other ways of course, but it does demonstrate
> the difference between static classes and the singleton pattern. The
> singleton pattern offers you a lot more possibilities.
>
> Spike
>
> On 10/29/05, JesterXL <[EMAIL PROTECTED]> wrote:
> >
> > To clarify:
> >
> >
> > class ServerConnection
> > {
> > private static var url;
> > private static var port;
> > private static var socket;
> >
> > public static function connect(p_url, p_port)
> > {
> > url = p_url;
> > port = p_port;
> > socket = new Socket();
> > socket.connect(url, port);
> > }
> >
> > public static function getData()
> > {
> > // Simple function that gets something from the server.
> > }
> > }
> >
> > Then to use:
> >
> > ServerConnection.connect(myURL, myPort);
> >
> > - Original Message -
> > From: "JesterXL" <[EMAIL PROTECTED]>
> > To: "Flashcoders mailing list" 
> > Sent: Friday, October 28, 2005 9:25 PM
> > Subject: Re: [Flashcoders] Newbie AS3 question
> >
> >
> > Naw, I don't know the exact way Singleton is implemented, hence my long
> > battle with finding clarification. It could of been solved in 10 seconds
> > over a beer, but email sux.
> >
> > I figured Math.abs was the Singleton patt

RE: [Flashcoders] Re: Newbie AS3 question

2005-10-29 Thread zwetan
> >>No need to get hyper about "this".
> >>The matter stays that "this" used to be essential in AS1, thus probably
> why
> >>people still like to implicate him in their code. But I agree that
> putting
> >>"this" in an AS2 Class should be used only when necessary.
>  >
>  > You do know that it (this.) is being added for you at compile time in
> AS2, right?
>
> Who cares? All that means is that there is no semantic difference
> between the two.

There is a difference for AS3 ! (and this is the correct behaviour)
http://livedocs.macromedia.com/labs/1/flex/langref/statements.html#this

"To call a function defined in a dynamic class, you must use this to invoke
the function in the proper scope"

zwetan



___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Re: Newbie AS3 question

2005-10-29 Thread Robert Tweed

Muzak wrote: [fixed top posting]

No need to get hyper about "this".
The matter stays that "this" used to be essential in AS1, thus probably why
people still like to implicate him in their code. But I agree that putting
"this" in an AS2 Class should be used only when necessary.

>
> You do know that it (this.) is being added for you at compile time in 
AS2, right?


Who cares? All that means is that there is no semantic difference 
between the two. Given that fact, you should use whichever syntax makes 
your own life easier as a programmer and is more maintainable. I started 
off using "this." a lot, partly because in Director there really *is* a 
semantic difference between explicitly using "me" (the equivalent of 
"this") or not - leaving it out is faster but can cause maintenance 
problems. You therefore get into the habit of always explicitly using 
"me." and I initially carried this practise across to AS2.


However as I tend to write a lot of static utility classes, I had to get 
out of the habit fairly quickly. I also found that I quite often wanted 
to take a bit of code from one class and put it into another class, 
which doesn't work if one is static and the other has "this." all over 
the place. After getting really sick of rewriting code all the time, I 
just dropped "this." entirely. My code does look cleaner that way, so 
I'm not going back.


If you really, really want to distinguish member properties from local 
variables, use prefixes. Or better yet, just write your code clearly 
(with comments if necessary) so that there isn't any ambiguity over what 
each variable is supposed to be for. There really is no good reason to 
include explicit "this" references everywhere.


- Robert
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Re: Newbie AS3 question

2005-10-28 Thread Muzak
You do know that it (this.) is being added for you at compile time in AS2, 
right?

Muzak

- Original Message - 
From: "Frédéric v. Bochmann" <[EMAIL PROTECTED]>
To: "'Flashcoders mailing list'" 
Sent: Saturday, October 29, 2005 12:53 AM
Subject: RE: [Flashcoders] Re: Newbie AS3 question


> No need to get hyper about "this".
> The matter stays that "this" used to be essential in AS1, thus probably why
> people still like to implicate him in their code. But I agree that putting
> "this" in an AS2 Class should be used only when necessary.
>
>


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Re: Newbie AS3 question

2005-10-28 Thread Spike
In a perfect world all methods would be short and succinct.

Unfortunately we don't live in a perfect world and it's sometimes more
confusing to split things up than to just leave longer methods. I've seen
plenty of code where 200 lines was the realistic minimum without creating
methods that had 15 or more parameters. Try implementing DES, or any other
encryption algorithm for that matter, and you'll probably find that your
methods are a lot longer than you'd really like.

I do agree though that use of this is a stylistic thing more than anything
else.

Spike

On 10/28/05, Martin Wood <[EMAIL PROTECTED]> wrote:
>
> > If you come along maintain someone's code 6 months from now and you find
> a
> > complex method of 200 lines of so, it's useful to have the this prefix
> to
> > distinguish between variables that are local to the function and those
> that
> > are available to the instance.
>
> true, but i would also immediately re-factor it into shorter, clearer
> methods.
>
> anyway, i think its more a matter of taste rather than good or bad
> programming.
>
> theres places where you need to use it, but otherwise do what you like. :)
>
>
>
>
> martin
> ___
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>



--

Stephen Milligan
Do you do the Badger?
http://www.yellowbadger.com

Do you cfeclipse? http://www.cfeclipse.org
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Re: Newbie AS3 question

2005-10-28 Thread Martin Wood

If you come along maintain someone's code 6 months from now and you find a
complex method of 200 lines of so, it's useful to have the this prefix to
distinguish between variables that are local to the function and those that
are available to the instance.


true, but i would also immediately re-factor it into shorter, clearer 
methods.


anyway, i think its more a matter of taste rather than good or bad 
programming.


theres places where you need to use it, but otherwise do what you like. :)




martin
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] Re: Newbie AS3 question

2005-10-28 Thread Frédéric v . Bochmann
No need to get hyper about "this".
The matter stays that "this" used to be essential in AS1, thus probably why
people still like to implicate him in their code. But I agree that putting
"this" in an AS2 Class should be used only when necessary.


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of A.Cicak
Sent: October 28, 2005 6:33 PM
To: flashcoders@chattyfig.figleaf.com
Subject: [Flashcoders] Re: Newbie AS3 question

Well, I dont agree, "this" keyword refers to current class, so its only more

typing to include it, and making code less
readable. Only reason keyword "this" exists is if you want to pass reference

to current object somewhere, in which case
you must use "this". To me using "this" in your code makes you look like 
wannabe-programmer, :) But I gues its matter of taste. btw, old VB does not 
have "this" keyword, and if you were reffering to VB(.NET), it is more OOP 
and more complex than AS3, so I gues programmers in VB.NET (if there are 
some, since C# is there) are not wannabe-programmers :)


"ryanm" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>> What I don't get is why it needs "this.addChild" instead of just 
>> addChild. I've been sick of the keyword "this" for a long time and have 
>> since avoided it in AS2.
>>
>> Any reason that it needs to be back in for AS3?
>>
>Maybe because it's one of the most useful scope references ever 
> invented?
>
>The fundamental concept that you seem to miss is that "addChild" is 
> meaningless by itself, it is a method of an object (in proper OOP 
> development), and if you just say "addChild", who is adding the child? You

> need a reference. You could do it like this if you like:
>
> class Game extends MovieClip {
>var world:MovieClip;
>var bg:MovieClip;
>function Game(){
>var GameReference:Game = this;
>world = new MovieClip();
>
>GameReference.addChild( world );
>
>bg = new MovieClip();
>world.addChild( bg );
>}
> }
>
>The point is, you shouldn't use functions that aren't attached to 
> objects, it's bad form, and it's thoroughlly confusing to people who later

> have to maintain your code. Besides, it makes you look like one of those 
> wannabe-programmer VB guys. ;-)
>
> ryanm
> ___
> 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

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Re: Newbie AS3 question

2005-10-28 Thread Spike
Forgot to mention, the other common place you'll see it is in constructors
or anywhere else you find yourself with method arguments that match the name
of an instance variable.

public function Person(fname:String,lname:String) {
this.fame = fname;
this.lname = lname;
}

Is it good practice? Probably not.

is it commonly seen in code? Definitely.

Spike

On 10/28/05, Spike <[EMAIL PROTECTED]> wrote:
>
> Passing a reference to the current object is not the only place where
> using the this prefix is useful.
>
> If you come along maintain someone's code 6 months from now and you find a
> complex method of 200 lines of so, it's useful to have the this prefix to
> distinguish between variables that are local to the function and those that
> are available to the instance.
>
> Using the this prefix in your code is more likely to make you look like an
> obsessively meticulious programmer than a newbie in my experience.
>
> If the IDE you use automatically highlights the instance variables then
> there isn't much point in using the this prefix that way, but it's certainly
> a valid excuse for doing it.
>
> Spike
>
> On 10/28/05, A.Cicak <[EMAIL PROTECTED]> wrote:
> >
> > Well, I dont agree, "this" keyword refers to current class, so its only
> > more
> > typing to include it, and making code less
> > readable. Only reason keyword "this" exists is if you want to pass
> > reference
> > to current object somewhere, in which case
> > you must use "this". To me using "this" in your code makes you look like
> > wannabe-programmer, :) But I gues its matter of taste. btw, old VB does
> > not
> > have "this" keyword, and if you were reffering to VB(.NET), it is more
> > OOP
> > and more complex than AS3, so I gues programmers in 
> > VB.NET(if there are
> > some, since C# is there) are not wannabe-programmers :)
> >
> >
> > "ryanm" <[EMAIL PROTECTED]> wrote in message
> > news:[EMAIL PROTECTED]
> > >> What I don't get is why it needs "this.addChild " instead of just
> > >> addChild. I've been sick of the keyword "this" for a long time and
> > have
> > >> since avoided it in AS2.
> > >>
> > >> Any reason that it needs to be back in for AS3?
> > >>
> > > Maybe because it's one of the most useful scope references ever
> > > invented?
> > >
> > > The fundamental concept that you seem to miss is that "addChild" is
> > > meaningless by itself, it is a method of an object (in proper OOP
> > > development), and if you just say "addChild", who is adding the child?
> > You
> > > need a reference. You could do it like this if you like:
> > >
> > > class Game extends MovieClip {
> > > var world:MovieClip;
> > > var bg:MovieClip;
> > > function Game(){
> > > var GameReference:Game = this;
> > > world = new MovieClip();
> > >
> > > GameReference.addChild( world );
> > >
> > > bg = new MovieClip();
> > > world.addChild( bg );
> > > }
> > > }
> > >
> > > The point is, you shouldn't use functions that aren't attached to
> > > objects, it's bad form, and it's thoroughlly confusing to people who
> > later
> > > have to maintain your code. Besides, it makes you look like one of
> > those
> > > wannabe-programmer VB guys. ;-)
> > >
> > > ryanm
> > > ___
> > > 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
> >
>
>
>
> --
> 
> Stephen Milligan
> Do you do the Badger?
> http://www.yellowbadger.com
>
> Do you cfeclipse? http://www.cfeclipse.org
>
>


--

Stephen Milligan
Do you do the Badger?
http://www.yellowbadger.com

Do you cfeclipse? http://www.cfeclipse.org
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Re: Newbie AS3 question

2005-10-28 Thread Spike
Passing a reference to the current object is not the only place where using
the this prefix is useful.

If you come along maintain someone's code 6 months from now and you find a
complex method of 200 lines of so, it's useful to have the this prefix to
distinguish between variables that are local to the function and those that
are available to the instance.

Using the this prefix in your code is more likely to make you look like an
obsessively meticulious programmer than a newbie in my experience.

If the IDE you use automatically highlights the instance variables then
there isn't much point in using the this prefix that way, but it's certainly
a valid excuse for doing it.

Spike

On 10/28/05, A.Cicak <[EMAIL PROTECTED]> wrote:
>
> Well, I dont agree, "this" keyword refers to current class, so its only
> more
> typing to include it, and making code less
> readable. Only reason keyword "this" exists is if you want to pass
> reference
> to current object somewhere, in which case
> you must use "this". To me using "this" in your code makes you look like
> wannabe-programmer, :) But I gues its matter of taste. btw, old VB does
> not
> have "this" keyword, and if you were reffering to VB(.NET), it is more OOP
> and more complex than AS3, so I gues programmers in VB.NET (if 
> there are
> some, since C# is there) are not wannabe-programmers :)
>
>
> "ryanm" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> >> What I don't get is why it needs "this.addChild" instead of just
> >> addChild. I've been sick of the keyword "this" for a long time and have
> >> since avoided it in AS2.
> >>
> >> Any reason that it needs to be back in for AS3?
> >>
> > Maybe because it's one of the most useful scope references ever
> > invented?
> >
> > The fundamental concept that you seem to miss is that "addChild" is
> > meaningless by itself, it is a method of an object (in proper OOP
> > development), and if you just say "addChild", who is adding the child?
> You
> > need a reference. You could do it like this if you like:
> >
> > class Game extends MovieClip {
> > var world:MovieClip;
> > var bg:MovieClip;
> > function Game(){
> > var GameReference:Game = this;
> > world = new MovieClip();
> >
> > GameReference.addChild( world );
> >
> > bg = new MovieClip();
> > world.addChild( bg );
> > }
> > }
> >
> > The point is, you shouldn't use functions that aren't attached to
> > objects, it's bad form, and it's thoroughlly confusing to people who
> later
> > have to maintain your code. Besides, it makes you look like one of those
> > wannabe-programmer VB guys. ;-)
> >
> > ryanm
> > ___
> > 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
>



--

Stephen Milligan
Do you do the Badger?
http://www.yellowbadger.com

Do you cfeclipse? http://www.cfeclipse.org
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders