Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 544
Marcelo Assis Avatar asked Dec 05 '22 14:12

Marcelo Assis


1 Answers

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
like image 155
Florian Salihovic Avatar answered Dec 31 '22 08:12

Florian Salihovic