You still don't like JavaScript?

Since the beginning of the HTML5 era, the market has needed a large number of JavaScript programmers. Many experts in other languages ​​began to reorient themselves to JavaScript in large quantities. Many times I saw how programmers with great experience scolded this, one of the most flexible and powerful language, without understanding its principles. In this article I will try to tell the essence and philosophy of JavaScript. There will be only 4 lines of code and many, many things that will not be written in textbooks. I promise that you will love this language, or at least become more effective in it. So let's go!

In turn, I will consider the main features of the language and the consequences of them. For example, JavaScript has a very low login threshold.. If you are familiar with C ++ or Java, then you can start quite well to program on it already 15 minutes after meeting. However, in order to do professional things, you need to read documentation, books on JavaScript patterns, communicate with more experienced colleagues. Most "pot-bellied and venerable Jedi of programming", of course, do not do this and blame the language themselves . Maybe it would be better if the entry threshold was higher? I think not (although there would be less criticism). Agree, it is much easier to comprehend the nuances when you can already write workable code.

JavaScript multi-paradigm language. It combines object-oriented and functional approaches. Structural programming and much more are also available. This is due to the highest flexibility of the language. Functions are objects of the first kind, variables can change type, objects can receive new properties on the fly. Is this good or bad? On the one hand, this creates problems for programmers who are used to certain languages ​​such as Java or C # to understand. But this is a small fee for efficiency. JavaScript, in most cases, allows you to create applications much faster than its strictly typing comrades. Let me give you just one example: I worked for a company developing a stock exchange terminal. It was necessary to create a desktop application on .Net C # and its exact copy on the web in JavaScript. The C # application was developed by a team of 12 programmers over two years, JavaScript is a team of 3 programmers throughout the year. We can talk about the difference in qualifications, that other factors may have influenced, but in one way or another, the difference is 8 times indicative.

How did you manage to achieve such a development speed? Most languages ​​put restrictions, supposedly for our own good. JavaScript does not recognize restrictions, you can change the code anywhere and anytime, including flexibly patching someone else's code. If we compare programming languages ​​with political systems, then, say VBA - this is totalitarianism, you are placed in a tight framework and can not even see all the sources. C ++ is a democracy, your freedom to wave your arms ends where your interlocutor’s nose begins. It is a good, powerful programming language that limits personal freedom for the common good. JavaScript is anarchy, you can change anything and no one will bother you, will not limit your freedom. The problem is that not everyone can live with complete freedom. We need a high inner culture so as not to steal a tractor from a neighbor, even if you don’t have anything for it. The same thing in code, for example, a common practice in JavaScript, is not to make private properties truly private. Ordinary (public) properties are created and an underscore is simply appended to their name. It is believed that other programmers will see this and will not use this property without special need. There are situations when laying on the values ​​of a private property or even modifying it is the most direct and correct way. Yes, there are exceptions everywhere, and JavaScript allows us to make them. In such cases, the programmer understands someone else's code and takes responsibility for the consequences. "He can break everything !?" - Adherents of other languages ​​are indignant. This is the next point. when laying on the values ​​of a private property or even modifying it is the most direct and correct way. Yes, there are exceptions everywhere, and JavaScript allows us to make them. In such cases, the programmer understands someone else's code and takes responsibility for the consequences. "He can break everything !?" - Adherents of other languages ​​are indignant. This is the next point. when laying on the values ​​of a private property or even modifying it is the most direct and correct way. Yes, there are exceptions everywhere, and JavaScript allows us to make them. In such cases, the programmer understands someone else's code and takes responsibility for the consequences. "He can break everything !?" - Adherents of other languages ​​are indignant. This is the next point.

There is no foolproof in JavaScript . We believe that all who work on the code are professionals and trust them. (pause) In a good JavaScript team, all programmers are at the same level. It can be low or high, but should not differ too much. Just as in a country where everyone steals, an honest person has a hard time, or in a society of decent people, a villain immediately gets what he deserves, just like in anarchic and uncontrolled JavaScript, juniors will break the code of the Jedi, and the Jedi will use absolutely incomprehensible ones juniors (but understandable for any professional) architectural moves, involuntarily reducing their productivity. So is it necessary to play it safe through the language, or is it better to just recruit the right people? Some blame JavaScript for what they call "bugs" and "irrationality." For instance:

'3' - 2 === 1;
'3' + 2 === '32';


This is exactly what it is advisable to read about type conversion after a quick start. For anyone, even a novice JavaScript programmer, the above examples are trivial and absolutely predictable. For 10 years, such things have never caused me problems. Another interesting problem that will return this code:

Math.max(2, []);
Math.max(2, {});


funny, but the first line will return 2, and the second NaN (Not a Number). Is this considered a mistake and illogical? Look above, there is no protection against fool in JavaScript, and if you pass an object or an array instead of a number, you can blame yourself only. In the same way, in C ++ you can write different garbage in memory and scold the language. If you have a gun, this does not mean that you need to shoot yourself in the foot.

A little about error handling - it is silent in JavaScript. This means that if the program can continue, it is likely to continue. Let's say you divided by zero, most languages ​​will throw an error and exit, and JavaScript will show Infinity and, possibly, even output it to the user interface as a string, if the program logic is intended to be so. Is this good or bad? I would say it allows you to createprograms with the highest level of stability . Failure of one element does not disable the entire system. Of course, it’s bad to show the user NaN or undefined, but this is much better than crashing, as we all have seen many times in many native Windows programs (and not only). We trust programmers and consider them professionals, therefore, in an ideal world, such situations should not appear. If the problem nevertheless arose, we have insurance, some margin of safety of the tongue. And, of course, in particularly important places in the program, we can manually check the values ​​for NaN, null, undefined, creating absolutely strict code.

For dessert, I’ll tell you a vision of the most controversial feature of JavaScript - prototype inheritance. If we talk about Object Oriented Programming, then generally speaking, it does not require that objects be necessarily declared through classes. Moreover, often we need some kind of object in a single copy, or we need to create several objects of the same type, but differing in some nuances - additional fields, flags, methods, etc. ... Often, creating classes is not necessary, this is an excessive level abstractions, and prototypal inheritance, or even creating objects on the fly, copes with such tasks. According to the principle of Occam’s razor, it’s not worth generating extra code if the task can be solved easier. This approach requires not only less text, but also less RAM and processor time. Classes do not come out of the box, but if you are writing a large enterprise application, writing 20 lines of your class implementation will not be difficult for you. If not, use one of the many libraries, in languages ​​like C ++ without libraries it will be much more difficult. Although some people prefer to write in pure JavaScript, it is not so difficult and has additional benefits.

We can say that JavaScript has an ideal, for our time, level of abstraction . You do not need to think about memory allocation, about fifteen sorting algorithms, about the type of operating system. You can concentrate on larger things, such as architecture or application logic. We can say that JavaScript is a language specifically for programmers, and not for hardware or system administrators. Cutting off the insignificant, you can achieve truly high performance and create high-quality and elegant software in an amazingly short time. Of course, if you feel completely free in the language.

The final and most important thing to understand about JavaScript is its dynamic typing.. This may seem strange, but I want to ask this question: why does the type system exist at all? Non-typed information prevails in the world of people: we cannot often draw a line, a story or a novel, an infant transforms into a child, then into a teenager. Is it possible to assign a type to these stages and does it need to be done? Data types are natural for the compiler, it is much easier to convert the code if we immediately know what primitives are served to us. But progress is moving forward, and I believe that languages ​​with flexible typing are significant steps forward. In the tradition of JavaScript, if you are missing class types, you can create them manually, write a wrapper, and enter restrictions. I worked for a game company whose product had very complex logic, and they wrapped classes with hard typing, multiple inheritance, and blackjack.tremendous level of introspection and you can adjust it for yourself in a very wide range.

To be completely honest, I’ll also say about the most significant drawback of JavaScript: professional programming in it is in many aspects more complicated than in Java or C #. If in strongly typed languages ​​you see at least the types of arguments that you accept into a function, then in JavaScript you need to keep all this data in mind. Sometimes you can’t even be sure what methods an object has. This requires a high level of concentration.and a good understanding of architecture. If you are ready for this and are ready to honestly learn the language, rather than blindly coding right off the bat, you will truly receive one of the most powerful tools for creating flexible and cross-platform software with high speed.

Also popular now: