JavaScript And Prejudice |
Written by Ian Elliot |
Saturday, 09 November 2019 |
It saddens me when people shoot off criticisms of languages they don't use and claim advantages for methodologies which are far from evidence- based. So imagine my disappointment when I encountered JS Johnny and an attempt to make static strong typing look good. Of course, I'm not saying that static strong typing is bad - it's just a simple compile time check to try and ensure that an object has the properties you think it does. What most advocates of this sort of typing seem not to understand is that the compiler is mostly capable of working out these problems at compile time without the complexities of typing and it doesn't catch runtime problems at all. Yes indeed, we have all discovered that we are using the wrong type in a function call. I've even seen a beginner search out the correct object to pass by trying alternatives until the type checker stopped complaining! This reveals quite clearly that often strong typing is a stopgap for not writing documentation and not reading it. I know that this is a very unpopular idea because strong typing has become the poster boy (thing?) for better programming for a long time now. What isn't well appreciated that there is very little evidence for this point of view. The few studies that there are suggest that if strong typing has any effect it is small and they don't consider any disadvantages. What is clear is that when you follow this particular approach a bit further you end up with a lot of "interesting" problems. After spending so much time enforcing strong typing you have to find ways of relaxing it so you can write type-free algorithms. The usual solution is to invent generics, something which Go is still struggling with it is so difficult. After generics you have to tackle variance and a range of other interesting problems, not to mention inheritance, interfaces and so on. All of these difficult and sophisticated concepts can be dispensed with if you just recognize that, like class-based inheritance, strong typing is easily replaced by other mechanisms. As I said, this is not a popular point of view and I don't expect many programmers reading this to agree with me but even if you don't there is a case to be made in this instance. This video attacks JavaScript for its lack of strong typing but the example it gives is wrong. In fact, understanding JavaScript and the way it works should instill awe into the viewer and not the loathing intended. Watch the video and then read my explanation of what is going on.
The argument is that if JS Johnny writes a function:
then the poor sap has no way of ensuring that the function is called with a text object. The example given is:
Oh no, JS Johnny you have passed the entire WINDOW!! No particular horror and if you try this out you will find that the program works and, as any competent JS programmer would know, it displays some text generated by the object's toString method. JavaScript converts objects to a value depending on what the requirement is - a string or a numeric value. In this case passing any object to the parseText function calls the toString method as soon as text is required - and this is how it works if the object is a String object. There is no sense in the String object being any different to any other object and restricting the parameter to be "text" is something from another language. As to passing a window to the function - well JS Johnny needs to read the manual and consider what parseText is doing. If it really is necessary to ensure that what is passed is a string object then test for it as the first line of the function. This protects against the error at runtime, which is much more important than the compile-time check of strong typing. Yes, there are things wrong with JavaScript, but this isn't one of them. If you are going to moan about a language, find out how it works before assuming it is a failed class-based language that was too lazy to introduce strong typing. This simply misleads beginners.
More InformationRelated ArticlesJavaScript The Language With Two Names JavaScript Is Basic's Offspring WAT! JavaScript, Ignorance And Prejudice To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info <ASIN:1871962579> |
Last Updated ( Saturday, 09 November 2019 ) |