قالب وردپرس درنا توس
Home / Tips and Tricks / What is the advantage of statically typed programming languages? – CloudSavvy IT

What is the advantage of statically typed programming languages? – CloudSavvy IT



Static typing

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 string or 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.

If you try to send junk argument functions, it accepts them;  if the function code expects a string instead of a number, it can give 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.

If you try to send C # something that 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.

The most important feature for developers is compile time error checking. If you code incorrectly and try to use a different type than intended, or try to call a method that does not exist, the compiler will shout at you. In a dynamically written language like JavaScript, it would simply throw an error while running – not good. IC # gives you alerts if you try to do something you should not, such as calling a non-existent method.

Compilation time error check in action

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.

With static typing, you can code more efficiently.

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.

For JavaScript, you actually have the option to use static typing or not. TypeScript is a superset of JavaScript supported by Microsoft, specially designed to add static writing to the language. It’s a preprocessed language, so everything’s JavaScript at the end of the day, but it provides all the IDE benefits of static writing. After all, JavaScript started as a basic scripting language, but today it runs some very complex web applications. The need for static typing in complex projects was clear and therefore TypeScript was born.

Is there any reason to prefer dynamic typing?

This problem is not completely black and white. Each language has different levels of statically controlled functions. These can even be added over time with linters and other static analysis tools, even for highly dynamic languages ​​such as JavaScript.

For the most part, the many advantages of static typing outweigh the few disadvantages, so if you have the option (as in the case of TypeScript vs JavaScript), most large projects opt for static writing. However, there are declining returns when it starts to become restrictive.

For some people, especially solo coders or start-ups who want to prototype quickly, there are many cases where dynamic typing may be superior. Dynamic typing is of course much more flexible. Removing the need to specify types allows faster writing of more concise code. This is especially useful for scripting languages ​​such as JavaScript and Python. For fast prototyping, dynamic typing can help a lot.

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.

IC #, if you want to send an int to a function that expects a float, you 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 int to 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.

You can add a number to a string and get a string as a result.


Source link