What is the difference between a static and dynamic programming language?

Some time ago, Facebook launched its own programming language, based on PHP. See: Technoblog: Facebook announces Hack, its own programming language .

Technoblog explains the difference between PHP and Hack as follows (emphasis added):

The main goal has always been to attack errors. In the official release, the developers explain that dynamic languages, such as PHP, facilitate the rapid development, but usually make error detection more difficult, since failures are only noticed when the program is running, which is particularly worrying in very large projects.

In turn, using languages of type static, such as C, developers can find errors before the actual execution of the code, with the possible disadvantage that languages like this are not as practical as the most current ones. As a consequence, the Hack was created to unite the best of the worlds.

Apparently, this means that the Hack supports declaring the return type of functions / methods, allowing IDEs to identify build errors in advance:

insert the description of the image here


  1. What is the difference between a static and dynamic language? Are there any other differences besides the one I was able to identify above?

  2. Advantages and disadvantages

  3. It can be said that are script languages always dynamic and vice versa? (See: what is a scripting language?)

Author: Vinicius Brasil, 2014-06-16

3 answers

I'm going to talk here in a way that people understand. If you go to study type theory you will see that some things there are different from this.

There is some confusion in several terms on this subject. In some cases there is no universally accepted formal definition.

Popularly languages are classified by their typing, so static languages are formally languages that have static typing. And dynamic languages have typing dynamic.

Some languages can be classified as dynamic because they have other characteristics. They allow arbitrary code execution(eval) or the transformation of existing codes at runtime. It is increasingly common for modern languages to allow these flexibilities. It does not matter the typing, so formally there are languages that are dynamic by this definition and static by typing.

Definition

Static

A basic definition of static typing that a programming language can have as a characteristic is that there is a check of the types used in data and variables to ensure that a type is always being used that is expected in all situations. This check is done in the source code by the build process. This analysis helps in calling type safety in the use of data by the program allowing the programmer to worry less about this issue. We compiler provides guarantees that some problems may not occur after the program goes through this check, i.e. errors are detected right before the program is actually run.

A variable cannot change its type.

However static typing can cause a false sense of security. Only a part of the errors can be discovered in advance.

Example (it's in C# but it could well be pseudocode):

var x = 1;
x = "1"; //erro de compilação, não pode trocar o tipo da variável

Dynamics

In dynamic typing this check also occurs but it is done on top of the data itself, since variables can contain any type of data. Of course, at a given time a variable can only contain one type of data and this is verified. But the main difference is that this check is done at runtime. This is done through an auxiliary infrastructure (a virtual machine or a library usually called a runtime ). It is common for the programmer to have to do their own checks on the program or on external test codes to ensure that all types are correct at the right times. The programmer has to worry more about types although in simple solutions it may seem that worry is not necessary.

What is dynamic effectively is the type of the variable. Understanding and documenting the types are still needed. There are languages that encourage documentation in the code with Hungarian notation .

Data needs to be represented concretely on the computer. variable is a design pattern (variable ). So it's an abstraction. Data cannot take multiple forms, at most it can be interpreted in different ways in specific cases. Variables can refer to different types. Therefore dynamic typing is usually a special case of static typing. Second Bob Harper ,one of the creators of the standard ML language, "a dynamically typed language is a statically typed language with only one static type".

In practice dynamic typing is an abstraction as well. There is an illusion that you can refer to a data that has different types but in practice there is only one marker and there is a pointing to a different static data.

The most common techniques to achieve this illusion is the use of a union type (union in C / C++) and / or pointers without type specification(void * in C / C++). All data will have some overhead of memory.

Example (it is in JavaScript but could be pseudocode, note that the syntax is identical to the previous example but the semantics are different)

var x = 1;
x = "1"; //operação normal, o tipo que era um number passa ser string

Performance

Another big difference is in relation to performance. Although it is possible to make optimizations in a program written with "dynamic language" to approaching or even exceeding the performance of programs written in "static languages", because of the difficulty of achieving this is usually not done effectively. In theory a JIT compiler has more conditions to optimize code with accurate information of how the program will run and even in practice can even achieve some better result in isolation.

On the other hand if you already know what you're going to deal with, you don't need to have anything in the generated code helping the operation in the program and the program itself does not need to have certain checks written by the programmer. If the check is done before execution a program with static typing does not need to have this job during execution.

Also does not help the fact that most dynamically typed programs will run on a virtual machine as this facilitates language development and allows some extra flexibilities normally desirable in "languages dynamics".

In practice languages are not usually 100% anything. Pragmatic tools know when to run away a bit from a concept to give better advantage to the programmer.

Hybrid languages

There are hybrid languages. In practice it is not possible to have both forms of typing but it is possible to use part of one or the other concept.

Static language with dynamic types

When a language is considered static, formally it has static typing, it may have the necessary infrastructure to store different data in a variable . But the data is saved in a different way than the other types. You'll have a static type that can store data dynamically, but the language still has static typing at its core.

Roughly speaking the language uses an extra data structure to store its type and indicate where the actual data is. It is usually done in the library and it usually has compiler help only to indicate that the check normally done should be relaxed since the check will be done by this library to a certain extent and also by the programmer to avoid that certain errors will be generated in runtime through this library.

Dynamic language with static types

Language with dynamic typing cannot effectively be partially static. After all a dynamic language, formally that the typing be dynamic, should always expect any type. If it starts waiting for a specific type and does the check before execution, it turns into "static language". It is not possible to reduce the level of abstraction.

It is even possible to provide a previous Type check as an additional feature of the language but it does not make much sense if it is not accompanied by the change in the way data is managed in memory. If the program needs to have its fixed types and guaranteed in advance it would be a waste to use a structure that allows to have several types. This structure has memory cost (tag of type, references to type in all situations*) and processing (extra indirection, selection of the specific data/Method to be used).

Doesn't make sense, but languages are doing this, almost all mainstream languages are adopting a form that approaches, without being, typing static.

Manifest typing

It is possible for a "dynamic language" to use manifest typing without changing its dynamic typing characteristic. The advantage is small or even questionable.

But there are also cases of languages that can compile parts of code with static typing and parts with dynamic typing. The interface between the parts need to be normalized one way or another. Interestingly, some prefer to define themselves as static or dynamic to try to instill one predominant culture and use the other as an exception. Purists, for better or for worse, consider it a bad choice. In fact there are two very similar languages in this case and not just one.

*some static languages have references for types when data is stored in the heap . And even in the stack the information will still exist in the code to allow reflection, but there will be no memory consumption in the stack to save the type.

Inference

Static typing does not mean that all types need to be explicitly declared. It is common, in most situations, for the compiler to be able to infer the type of a variable according to its assignment or even its use (less common).

The great advantage of static typing is more in the fact that the variable cannot change its type. This really helps development a lot. Having to write the type explicitly helps little or nothing. Some think it gets more readable when the type is written, others think it's just compiler deficiency.

Then it is possible to have a "static language" that uses implicit typing by partially reducing the ceremony that these languages usually have.

Hack

PHP has the possibility to do a type check before execution but this helps little because the language is usually executed from form interpreted and mainly because it can not check all types, all situations, so you have no security at all. Security is defined by the weakest link. During execution everything is dynamic.

I don't know the Hack language enough to tell you how it works but it looks like it has static typing.

Eventually one can use the type mixed which is that data structure that will allow any data to be stored there. It is likely that the compiler treats this type differently. The program treats data dynamically by exception, the programmer says that there anything can happen because this is what he wants.

For several reasons it seems that Hack is PHP that drinks in the water of C#, it seems to me that mixed behaves like the dynamic of C#.

Advantages and disadvantages

Often the advantages and disadvantages vary according to the eyes of the beholder. Programmers disagree on what is really a advantage, not least because almost everything is a matter of trade-off, so [it's easier to talk about differences. Some of them can be:

Dynamic Vs Static

The main advantages of" static languages " are security, performance and development time support (refactoring, code completion, auxiliary information, code coverage, etc.).

"dynamic languages" are flexible , rapid prototyping, concise .

It is good to understand that the more complicated bugs remain complicated equally in both types.

There is certainly an advantage to "dynamic languages" when thinking about the development of the language itself. Defining the language and creating a basic implementation is much simpler than a "static language". But to create a powerful implementation that can solve some of your disadvantages, it becomes a difficult job. To date, no one has solved all the disadvantages.

Strong and weak typing

Sometimes these concepts are confused with strong and weak typing. This is partly because the strength of typing is not well defined and is not universally accepted.

Strong typing is usually the characteristic that does not allow the same data to be treated as if it were of another type. It is very common for static languages to have strong typing. But there are exceptions.

This gives the code more robustness.

C, for example, allows one data to be accessed/interpreted as if it were another. Can, for example:

  • write a int and access it as if it were a pointer.
  • write a float and access as if it were a int. True, the result will be catastrophic in this case, but it is possible.
  • get a 0 and be considered false or other numbers (no matter what type) to be interpreted as true in operations that require a boolean.
  • write two short in sequence and read as one int. Probably nothing useful will be obtained but it is possible.
  • record "SOpt" and read this as if it were a int, I don't know why.

C is a weak typing language. C++ too, although it tries to reinforce a style where this is not so used.

C / C++ compilers try to prevent this from being used abusively.

Hence we conclude that type security is not an inherent feature of so-called static languages. Security can be broken by other factors. type safety is another different concept that can be mistakenly confused with strong typing and static typing.

Many "dynamic languages" have strong typing, but others have weak typing, typically creating implicit coercions. Coercion is common in some situations under defined rules. Example: "1" == 1 is true and 1 + "1" gives "11".

Implicit coercion has the advantage of making the code slightly shorter. This is often characteristic of script languages, where code size makes a difference. So, languages made to develop applications, as opposed to scripts, should not have this feature (Hello PHP).

The definition of these terms do not help much and say that a language is weakly or strongly typed in an exclusive way is also not usually true.

In general we can say something like this:

  • variable has type = > typing static
  • variable without type = > typing dynamic
  • value has type = > typing strong
  • value without type = > typing weak

Languages of script

It becomes increasingly difficult to define script languages. They are usually dynamic but nothing prevents them from being static.

Dynamic typing gives more flexibility and helps make small applications quickly. These languages often require little ceremony and dynamic typing provides little ceremony in typing. But it is more a matter of adequacy than a requirement.

Dynamic languages usually perform more form interpreted than compiled and is another indirect factor that helps to be used to make scripts. But it's just a facilitator, again, it's not a requirement.

No Language wants to lose space so more and more static (typing) languages allow dynamic typing characteristics, reflection, conciseness, and simplified execution (illusion of interpretation). Thus they can be considered from script as well, although it is not his main focus.

In addition to adequacy, the characteristic of script is more tied to the implementation than to the language itself.

A reflection

If a language allows its static types to have values that are invalid or do not match what is expected in this type, a null for example, does it still have static typing? It seems so, but there is doubt whether they should. Breaks the security of types and in a certain way can be interpreted as the given it could have two types. It forces a check on the runtime to ensure that there will be no problems with the type.

Which is better?

The question you don't want and probably will never shut up is: which is better?

It is obvious that there is not universally one better than the other. We can always talk the cliché that there is a better tool for a problem.

In practice I see that the choice falls, most of the time, on taste and experience. And this is good. I often say that before you choose the best tool for the problem, choose something that you know and feel comfortable using: "the best tool is the one you know how to use". It is the old dispute between engineers who want everything to be perfect and administrators who want the best result to come. An electric saw cuts wood faster and more accurately. A weekend Joiner can get hurt with a saw electric.

Of course there are cases that technically one is better than the other but this is getting more and more subtle.

My personal observation is that "dynamic language" programmers often think less about the problem and create overly simplified designs that bring future problems. But there are cases that a better design is not advantage.

On the other hand I see that programmers of "static languages" tend to think too much about the problems and they create overly complicated designs without bringing many future advantages and not solving all future problems.

Note that this does not define the quality of the programmer and let alone that 100% of programmers are so in 100% of cases. There are cases that the opposite happens. And it really should. When programming in "dynamic languages" early planning is often more important.

I cannot say that typing itself is responsible for the trend (if it is true). It may be the kind of programmer that typing attracts. It is the surfer going to the sea with waves and not the sea with waves making the subject turn surfer. Maybe people choose typing for the wrong reasons.

What I see happening a lot is that programmers who use "static languages" do not use the dynamic facilities existing in these languages unless there is no other way. And programmers of "dynamic languages" think that checking the contracts should never be made within code (in fact these languages do not usually provide facilities for this). I don't know if it should be like this.

PDF with something more formalized to read . And a very complete publication and apparently very well founded.

See also: what is typing style?.

 79
Author: Maniero, 2020-09-04 12:52:38

What is the difference between a static and dynamic language?

There is no concept of "static language" or "Dynamic Language". What the original English article speaks - and what the Portuguese translation failed to express - is in languages with static vs. Dynamic Type System.

Traditionally, dynamically typed languages allow for rapid development but sacrifice the ability to catch errors early and introspect code quickly, particularly on larger codebases. Conversely, statically typed languages provide more of a safety net, but often at the cost of quick iteration. We believed there had to be a sweet spot.

In a static type system, each variable has a type (class, domain), and the compiler tries to ensure that only values of that type are assigned to the variable - and that only Operations applicable to that type are called having the variable as a parameter. If a variable does not have the correct type (i.e. identical to the expected type or subtype), the only way to use it in that context is through an explicit type conversion (cast).

In general this implies declaring the type of every variable, member, parameter, or return value, but this is not always true. Languages that use type inference allow the type declaration to be omitted in certain circumstances, and the compiler tries to figure out what the correct type is based on the context where the variable was created. An example is the var statement of C#.

In a dynamic type System, variables do not have types. The values , of course, still have a type - be it a primitive type, an instance of a Class, A "list of properties", etc. But since a variable keeps reference for a value, that reference can be used in any context and serve as a parameter for any operation - it is the responsibility of the programmer to ensure that only values of the right type are used (it would be more accurate to say: from the domain right; because dynamic typing allows for example to create functions that accept more than one different type as a parameter - and behave differently depending on the value received).

Advantages and disadvantages

Languages with dynamic types tend to be more concise, because or if you use a simple keyword for every variable (e.g. JavaScript var) or no keyword is used when creating it (e.g. Python). This makes quite a difference in particular when types have long names (e.g. VirtualMachineDeviceRuntimeInfo) or when - if it were static typing-generics are involved (e.g. HashMap<Integer,List<String>>).

Note that with type inference one can [partially] retain this advantage without giving up static typing.

On the other hand, languages with static types tend to to "protect the programmer more from himself". Instead of depending on the programmer being careful to always call the right operation with the right value, the compiler itself makes sure the type is correct and - if it isn't - throws an error before the program is even running. At least that is the theory; in practice, it is impractical if you use a type system that is restricted to the point of never accepting anything that is not part of the domain (e.g., the type int can be used as a denominator of a division? Not if its value is zero...)- and languages with more "complete" types tend to be progressively more difficult to use in practice (e.g. Haskell).

These are the main advantages of each (and correspondingly disadvantages of the other). Moreover, there is a lot of opinion involved, and it is difficult to argue objectively. For my part, I see the type as a meta-given , and I think it's desirable for a language to support not only that but also other meta-data (contract programming) - but in an optional way.

Can it be said that scripting languages are always dynamic and vice versa?

No. These concepts are orthogonal. Being "scripted" or " standalone " has no relation to static or dynamic typing, nor is there any relation between these and: 1) compiled vs. interpreted; 2) strong or weak types ; 3) high or low abstraction level; etc.

If there is a correlation between scripting language and dynamic types this is probably due to the advantages and disadvantages mentioned above: one wants the effort to produce a script to operate/automate a system to be small, and in general there is no distinct build vs. run phase (in the sense that once the script is ready, it is expected to run immediately-in production).

On the other hand, when using a language standalone if builds a system, the code typically goes through several iterations of compilation, testing, refactoring, etc - so that "speed" in putting the code into execution is not that critical. In this case, a compiler that is more effective at finding errors proves to be a more desirable feature.

Despite this correlation, exceptions exist for both sides. Incidentally, the very classification of a language as being or not "scripting" does not it is something so well outlined: see for example that C was once considered "the scripting language of Unix"... And JavaScript is no longer just "the scripting language of the browser": see its use on the server side (node.js), and in other contexts.

 25
Author: mgibsonbr, 2014-06-16 12:10:54
  1. In a static typing language, the type is defined at compile time, while the dynamics are defined at runtime. In C, which is a static typing language, for example, you define a variable explicitly indicating its type. If you try to assign a value to this variable that is incompatible with the defined type, the compiler will report an error. In a dynamic typing language, such as JavaScript, a reserved keyword is used to create a variable "var" and depending on the value that is being assigned to this variable, under the cloths, the type will be set.

  2. The advantage of a dynamic typing language would be speed in coding, without worrying too much about types. A possible problem would be in operations between types that contain incompatible values for certain operations. The result would only be discovered at runtime, i.e. if there is an error, depending on the type of error, you have to in the right place and at the right time to realize that he is there.

  3. I don't think there's a rule. The answer to this question can be in the compilers and interpreters. Compilers like static typing; interpreters, dynamic typing.

 0
Author: Alexandre Tavares, 2019-05-12 17:57:30