Static typing is a programming language function that requires you to strictly define the structure and data types of the code you are writing. Although there is often more work for the developer, it leads to much cleaner and more understandable code bases in the long run.
What is static typing?
In a dynamically written language, you define everything with
var. There are no predefined types like
int; the variable simply contains a value:
var num = 5; var str = "hello";
This may not be a problem for local variables like this, but as you begin to work with more complex data, you begin to encounter a problem. Functions do not give you any information about which arguments to send it, other than the function names themselves. If you try to send junk arguments, it will accept them, and if the function code expects a string instead of a number, it may throw an error.
But in a statically written language like C #, the types are set before you press compile. It is immediately much clearer what the function wants from you, and if you try to send it to something it did not ask for, the compiler will throw an error.
In addition, it is also much clearer what the function actually does, because the function will have a return type. Without first understanding the code base or even taking a single look at the source code, you can conclude what to do from the return type, function name, and input parameters.
This static typing applies everywhere and usually quite aggressively. When defining a variable, you must enter a type name. All functions must have fully specified type names for the arguments and the return type. If you switch from dynamically written languages, you must not just write
var without thinking.
Why is static typing useful?
If this seems like extra work, it really is. But it has some great benefits in the long run.
This helps catch bugs early and reduces the number of buggy commitments that your developers will make.
In addition to capturing errors in sloppy code, static typing can compensate for extra work by allowing you to code more efficiently in the future. When you use a function or try to access the properties of a class, your IDE can find out what types the function takes or what methods the class implements. This makes it easier to work with code that you did not write.
Static code can also be run faster. It does not matter if the language is interpreted or compiled. However, knowledge of static types can enable further machine code optimization, so that your program does not use more memory or clock cycles than it really needs.
Is there any reason to prefer dynamic typing?
Because nothing is specified at compile time, dynamically written languages often use duck typing to determine what an object can do. Checking for a method before it is called allows different types of inputs to be sent to the function, which can enable more flexible polymorphism.
Overall, these are all personal preferences, so it is not possible to say that static typing is better all the time. It is about you and your project’s needs and also other functions in the languages you are considering using.
Strongly typed against weakly typed
Although it sounds very similar, “strong versus weakly typed” and “static versus dynamically typed” are two different concepts.
Strong writing means that the language does not make many implicit type conversions or type constraints. For example, C # is a strongly written language. If you want to send one
int to a function that expects a
floatyou get an error.
This can be remedied with an explicit conversion, called cast. Strongly written languages require more cast. Weakly written languages make automatic conversions. Again, it’s not black and white here, and some implicit conversions are found in C # and other heavily written languages, and you can also define your own implicit conversions between custom types.
In general, strong writing leads to less confusion overall, but implicit transformations where it makes sense are useful. For example, conversion
float automatic is usually good, as no data changes other than switching to another type. Convert one
float to one
int would need some form of rounding to get rid of the decimals.
Weak typing can lead to confusion in some extreme cases. For example, JS is known for its generous implicit conversions: You can add a number to a string and get a string as a result. But if you multiply a number and a string, it analyzes the string in a number and returns
NaN if it can not be analyzed.