Sunday, 30 August 2009

ActionScript is not Java!!

When learning a new language (not programming literally a new language), the best thing is not to compare it with other languages and your native language unless they are really very common. Each language has its own grammar, own way to express things and different prononciation. If you try to learn the new langauge base on another one, you will probably end up speaking french as an english men or vice versa. Languages have their own tradition, heritage and culture, which will greatly differ from one to other.

Programming languages are also same. You may object this saying some of them share the same basics, which I also agree (ex. java and c#) but they still differ. If you move from one to another with your native tradions you will never look like a native speaker on your new platform. Its not wrong to use either camel case or hungarian case depending on your tradion but it will definitely reveal where you come from, and for sure you will seem like some one out of the community. Curly braces are also the same, you can use them just after the declaration or the next line but with other classes or methods written by others around you will definitly seem and english men in new york.
I face this issue when i try to teach Flex to someone from Java or .Net background. It is hard to accept your beloved arrayList to be some else acting a bit different although looking quite same. If you are just playing the language then its fine, but if you are seriously interested then here comes the problem. Actually more serious problems are ahead if you do not realize you are in a new world. Here is a great example from Chet who is a Java and Flex expert..

Basically his examples shows the difference of variable decleration and scopes between java and flex.

What do you think this prints out?

            var i:int;
for (i = 0; i < 2; ++i)
var blah:Object;
trace("blah = " + blah);
blah = i;

Or how about this?

            for (i = 0; i < 2; ++i)
var blarg:Object = null;
trace("blarg = " + blarg);
blarg = i;
In actionscript the scope of the variables blah and blarg is function level, which is diffrent than Java. In Java we would expect the reference to be renewed in each iteration. However in Flex since each iteration refers to the same object it is not null anymore after first loop. Which will result an output of;

The first example prints out:

    blah = null

blah = 0

and the second example prints out:

    blarg = null

blarg = null

More details go and check his post! But you might have already noticed him placing the curly braces in the new line which is not a Java tradition but which is actionscript.
Always try to consume as much the culture of a new language, first it will make you close to be a natuve speaker, second you will understand how it works better and finally being multi cultural will extend your ways to think and remember no language is perfect..