There is a class of languages ​​where typing is dynamic, for example javascript, vb and so on.

I, as an adherent of strict typification, do not understand the sacral meaning of this.

Well, yes, it seems to be fun to build a class in JavaScript on the go. However, what's the point? After all, I can declare everything here and now and build the necessary models, because I must know in advance what I will have to work with.

In T-SQL, dynamic queries are justified, but again, it all comes down to a finite set of some kind of filter filters.

In my opinion one cons:

  1. IDE, as a rule, cannot immediately issue errors during startup
  2. Crookedly works auto-substitution options, as the variable is dynamic
  3. And in my head you need to remember that this variable is of such and such type, and here it has changed the type for another ...
  • Try to write in such languages, immediately begin to notice the advantages, such as implicit type conversion. for me, when it was exactly this, it became a plus, although at the moment it is again a minus =) - Vladimir Klykov
  • Initially, most languages ​​with dynamic typing were intended either for writing scripts or for research and prototyping. In general, to quickly write small programs with data without "random" errors. Due to their success, they became popular and (hereinafter CoC does not allow) programmers began to use them everywhere. - avp
  • 12
    Dynamic typing is needed to make mistakes, eat resources and incite holivars. - Alexander Petrov
  • 2
    In essence, dynamic typing is syntactic sugar, because functions for strict type conversion are usually found in all languages, but they do not have to be used if the typing is dynamic. Syntactic sugar is needed to write tritely faster. Yes, it creates problems, but also saves time (which is money). You always have two scales of scales - at one time of writing code, at another its quality, so one cannot definitely say that dynamic typing is worse or better than strict typing. - Beast Winterwolf
  • 2
    @ iluxa1810 I do not understand what answer do you expect to receive? Why they do it this way - because it is convenient for some people when an IDE checks everything for them in advance, for others it is not convenient. And writing in a language with dynamic typing is an order of magnitude faster and you can afford more interesting tricks - Mike

3 answers 3

The fact is that the lack of types kills many birds with one stone. The main ones are the readability of the code, and an increase in the simplicity of programming in general. The first is very important strategically, so that with a large amount of code, long development, the project is still readable and expandable, the second - tactically - in order to quickly solve problems. But an adherent of strict typing may not feel these advantages at first, for example, I myself once switched from C ++ to JS and PHP - and probably even a year was indignant. I will try to explain ..

Brain Resources . The presence of types in the code creates an additional nonweak semantic load, and requires serious brainstorming to memorize the hierarchy, to read the code, to prescribe these same types for each sneeze in the code. In the compiled code, the types used to carry the main meaning behind them - the maximum speed of the program, the minimum consumption of the OP. For scripting languages, such a goal is usually not worth it (and modern compilers are not too much eaten away in the absence of types), and therefore the decisive factors of language typing are precisely the types of brains of programmers in the context of scripting language tasks.

Structural complexity . The scripts emphasize the possible complexity of the script, with high readability. And here JS has got better - reading any JS-code you can see how attention is diverted from the object-type component, and focuses on the structural component: nested closures, functional programming, asynchronous programming. And there would be in JS types - of course, this effect of perception of the code would not exist.

Readability . If you correctly name the variables in the JS code, the code will be read like a book, despite the structural complexity. But when types are added to variables, readability loses much: the reading programmer gets a lot of unnecessary information. Here in life we ​​say Убери яблоко в холодильник and not Убери красное круглое яблоко в большой серебристый холодильник , since the first is easier to perceive. For example, I would compare the reading of the JS code of any widely distributed library with the reading of Pushkin’s poems, whereas the reading of typed C ++ code with the reading of Homer's Iliad (now).

About the simplicity of programming without types . It turns out that without types, and with developed functional tools: half of the patterns are not needed, the second half of the patterns are written in three lines instead of 15. The classical implementation of GoF patterns in typed languages ​​today is more like crutches, which simply exist to compensate for the lack of flexibility in the presence of strong typing - respectively, they are unreadable, you need to seriously deal with them, fill yourself with the questions "why is that?" “is it a facade or an adapter?”, etc. In JS, however, there are plus to the old completely different approaches based on closures, encapsulation, and AF. With the implementation of one-and-the same task in the style of OOP-classics on TS, and in the style of OP on JS, the result is:

  1. The code on JS turns out to be smaller 3 times, and more readable than a code with strict typing on the same TS.
  2. The programmer implements the task faster, in proportion to the amount of code.

Plus JS works mainly with a tree-like database called DOM - and its task is basically managing this DOM over time. That is, no serious OOP structures are required, the built-in one is more than enough: processing DOM events, asynchronous JS capabilities. This, coupled with the lack of typification, makes the JS code "the quintessence of control logic." "Ease of programming" does not mean that JS programmers will have a freebie, it means that a good programmer will have time to do 3 times more.

What does js lose without types?
The first thing that JS loses is the automatic validation of incoming parameters of a function - this is almost the main role that types perform in modern scripting languages. Parameters can be validated manually in JS, but this is done extremely rarely, unless it is often checked for a void if (!param1) return false; . Why not do? Because JS carries in itself a style of code in which a type is not the main characteristic of a variable - and programmers quickly understand this.
The second thing that JS loses is the inability to build a classical OOP architecture. No, of course, crutches and bicycles can certainly make a strong similarity to OOP - once (far from ES6), the creators of mootools asked this question, and today 90% of JS programmers facing mootools consider it the worst framework of all time and all peoples) Why? Yes, because in JS the richest functionality, including with regards to encapsulation (so necessary for large projects), a lot of ways to make “safe” expansion points for a program, a lot of “its patterns,” it doesn’t need OOP to make any quality extensible program scale. The typification, the more strict, is the locomotive of the “grandfather's PLO” - and, accordingly, the oppressor of flexible ways of constructing programs.
The last is when working in IDE autocomplete and the "transition to the class" work poorly. This happens, but if you decide to write OOP-like code in js, then use JSDoc to help the IDE. At the same time, poor navigation is a problem of the IDE itself, for example PhpStorm (WebStorm) - it works with navigation in JS well and without JSDoc, and promises that it will soon be excellent.

I described it in my own words as best I could, but it’s difficult to explain the many advantages of the lack of types to a programmer in strongly-typed languages: it’s almost a message to the Buddhist that Christianity is cool :)

UPD but in order not to turn the answer into malicious holivar, it should be noted that types are also useful for the same code readability, for example, in function arguments. But when types are used sometimes, and not in 100% of the code. But if you allow only types in arguments in JS (for example, in TS), programmers will immediately start abusing OOP, and kill the established style of the language, and at the same time all the advantages of JS - I think this is the cause of the typology EcmaScript. Strict typing is useless and even harmful, but only if we are not talking about maximum performance: then there are no questions, strict typification will win.

  • Comments are not intended for extended discussion; conversation moved to chat . - Yuriy SPb
  • “But when types are used sometimes, and not in 100% of the code” - many modern PL with static typing have all sorts of different auto, let and var, and absolutely do not need to prescribe types everywhere - andreymal
  • “Strict typing is useless and even harmful” - weak typing is useless and harmful: nothing good is that you can add a number and a string, and that adding two arrays results in a string, no, and this is a permanent source of errors - andreymal
  • (although in general, according to Wikipedia, there is no strict typification, it can be strong or static) - andreymal
  • @andreymal those who talk about errors - have not yet experienced the technology, there is nothing more to add. And your comments (including those that were taken to the chat) are very superficial, in response I tried to state the deep essence of the use of the lack of types, the essence of which I (and other colleagues) had already formed in my head for 8 years of programming in JS, which will hardly be stated in books and articles. Now JS is my favorite language. And before that, it was professional Coding in C ++ and spat on the lack of types, just like you. I can no longer spend time on discussions, add your answer - and probably your answer will be better and more accurate. - Goncharov Alexander

Compact code, mostly. Suppose we have several classes that occupy a completely different place in the inheritance hierarchy, but are united by the presence of the common SaveToFile method, which takes one string argument (file path). Then we want to write a function that will save to the file all elements from an array of arbitrary objects.

In a language with static typing, we will need to declare the interface, mark all classes with the SaveToFile method as implementing it, and use a type conversion:

 interface ISaveable{ void SaveToFile(string path); } void SaveAll(object[] array, string path){ for(int i=0; i<array.Length; i++){ ((ISaveable)array[i]).SaveToFile(path + '_' + i.ToString()); } } 

In a language with dynamic typing, everything will be much shorter:

 function SaveAll(array, path){ for(i=0; i<array.length; i++){ array[i].SaveToFile(path + '_' + i); } } 

The brevity of the code is not always welcome, but for scripting languages ​​in which programs are often written "at one time" or when saving on the size of the source code (browser-based JavaScript) is important, this advantage may come to the fore.

For details on comparing dynamic and static typing: Is there a real advantage to dynamic languages?

  • 2
    '_' + i will not work in some dynamically typed languages, for example in Python - andreymal
  • andreymal is right. In the case of path + '_' + i, Python will assume string concatenation, and will not be able to apply it to the number (int) and therefore this can only be done if you cast int to str - str (i) - AtachiShadow

High-level languages ​​are invented to speed up the process of achieving the final result.

Quote from here. Languages ​​with dynamic typing are needed precisely for the same: in order to get a working result faster.

Working means earning money. Programming is, after all, a practical discipline, which means that in one way or another comes down either to saving the time of the user or developer, or to the money earned or saved.