Marcelo Assis
Marcelo Assis

Reputation: 5204

Is there any advantage in specifying types of variables and return type of functions?

I always set the types of my variables and functions, a habit I brought from my Java learning, seems the right thing to do.
But I always see "weak typing" in other people's code, but I can't disagree with that as I don't know what are the real advantages of keep everything strong typed.

I think my question is clear, but I gonna give some examples:

var id = "Z226";

function changeId(newId){
    id = newId;
    return newId;
}

My code would be like this:

var id:String = "Z226";

function changeId(newId:String):String{
    id = newId;
    return newId;
}

Upvotes: 4

Views: 259

Answers (4)

cleong
cleong

Reputation: 7626

While the points in the other answers about code hinting and error checking are accurate, I want to address the claim about performance. It's really not all that true. In theory, strong type allows the compiler to generate code that's closer to native. With the current VM though, such optimization doesn't happen. Here and there the AS3 compiler will employ an integer instruction instead of a floating point one. Otherwise the type indicators don't have much effect at runtime.

For example, consider the following code:

function hello():String {
    return "Hello";
}

var s:String = hello() + ' world';
trace(s);

Here're the AVM2 op codes resulting from it:

getlocal_0 
pushscope
getlocal_0 
getlocal_0 
callproperty 4 0 ; call hello()
pushstring 12    ; push ' world' onto stack
add              ; concatenate the two
initproperty 5   ; save it to var s
findpropstrict 7 ; look up trace
getlocal_0       ; push this onto stack
getproperty 5    ; look up var s 
callpropvoid 7 1 ; call trace
returnvoid 

Now, if I remove the type indicators, I get the following:

getlocal_0 
pushscope 
getlocal_0 
getlocal_0 
callproperty 3 0 
pushstring 11 
add 
initproperty 4 
findpropstrict 6 
getlocal_0 
getproperty 4 
callpropvoid 6 1 
returnvoid 

It's exactly the same, except all the name indices are one less since 'String' no longer appears in the constant table.

I'm not trying to discourage people from employing strong typing. One just shouldn't expect miracle on the performance front.

EDIT: In case anyone is interested, I've put my AS3 bytecode disassembler online:

http://flaczki.net46.net/codedump/

I've improved it so that it now dereferences the operands.

Upvotes: 1

net.uk.sweet
net.uk.sweet

Reputation: 12431

As pointed out by florian, two advantages of strongly typing are that development tools can can use the information to provide better code-hinting and code-completion, and that type, as an explicit indicator of how the variable or method is intended to be used, can make the code much easier to understand.

The question of performance seems to be up for debate. However, this answer on stackoverflow suggests that typed is definitely faster than untyped in certain benchmark tests but, as the author states, not so much that you would notice it under normal conditions.

However, I would argue that the biggest advantage of strong typing is that you get a compiler error if you attempt to assign or return a value of the wrong type. This helps prevent the kind of pernicious bug which can only be tracked down by actually running the program.

Consider the following contrived example in which ActionScript automatically converts the result to a string before returning. Strongly typing the method's parameter and return will ensure that the program will not compile and a warning is issued. This could potentially save you hours of debugging.

function increment(value) {
    return value + 1;
}

trace(increment("1"));
// 11

Upvotes: 3

Pixel Elephant
Pixel Elephant

Reputation: 21403

You get performance benefits from strongly typing. See http://gskinner.com/talks/quick/#45

I also find strongly typed code to be much more readable, but I guess depending on the person they may not care.

Upvotes: 4

Florian Salihovic
Florian Salihovic

Reputation: 3961

Yes, the big advantanges are:

  1. faster code execution, because the runtime know the type, it does not have to evaluate the call
  2. better tool support: auto completion and code hints will work with typed arguments and return types
  3. far better readability

Upvotes: 7

Related Questions