Shop OBEX P1 Docs P2 Docs Learn Events
The Official JavaScript Religious War Thread. — Parallax Forums

The Official JavaScript Religious War Thread.

Martin_HMartin_H Posts: 4,051
edited 2015-06-05 02:18 in General Discussion
This is the official JavaScript Religious War Thread. If the topic comes up in another thread, please feel free to reply to this post. But me first because I started it.

JavaScript follows a "maximum surprises" design philosophy that rejects years of accumulated programming wisdom, only to serve as the anti-pattern to prove that wisdom correct. Let me enumerate a few of its toxic flaws:

Numbers in JavaScript are "double-precision 64-bit format IEEE 754 values", according to the spec. This has some interesting consequences. There's no such thing as an integer in JavaScript, so you have to be a little careful with your arithmetic if you're used to math in C or Java (i. e. a real programming language).

A central value of programming is to find mistakes as early as possible. JavaScript's lack of type safety means that a number of trivial errors won't be found until runtime, or worse production.

Modern programming patterns (e. g. inheritance, interfaces, encapsulation) are missing, instead it uses a prototype based object system that is brittle, and fits its "maximum surprises" philosophy.

It doesn't have the concept of multiple threads, but uses event handling with embedded lambda functions. This results in daisy chained function definitions that get ugly quickly.

A variable can be undefined, defined and null, or defined and have a value. As if null pointer exceptions weren't bad enough. But for extra joy a knuckle head can define the word "undefined" and now your test against undefined will fail. Surprise!

It's such an unsafe language that companies that are some of its biggest users are looking to improve or replace it. Microsoft with its typescript preprocessor and Google's new scripting language Dart.

I can come up with more, but I hope to convince young professionals that although they have use this language in a browser, please understand that it is completely broken and should be avoided otherwise.
«13456712

Comments

  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-12-07 07:43
    I guess I will just stay away from JavaScript. Plain old Java was traumatic enough with all the directory confusion about getting its tool chain to work properly.

    At least with Forth, the short-comings point you toward learning to program in PASM. With JavaScripts, I suspect the short-comings will point you toward seeking extra-terrestrial extra-sensory assistance.
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 07:51
    Oh cool, a language war zone. A no man's land, full of mines and barbed wire, Where only fools tread !

    There are is a lot of inflammatory language there and biased value judgements along with some factual inaccuracies, as is fitting of a language war post :)

    About those integers:

    Those 64 bits doubles can hold integers up to 53 bits. Not bad. It's not so hard to ensure they are integers at all times.

    If you do integer arithmetic in C, Java, Spin etc you have to be a little careful, A + B may well give the correct answer C. Unless there is an overflow of course, which you won't know about until run time or production because the compiler cannot help you.
    ...lack of type safety means that a number of trivial errors won't be found until runtime, or worse production.
    If you are finding type errors in production then your testing is grossly inadequate. If you testing is grossly inadequate the compile time checks of a statically typed language won't save you. You will have plenty of other errors compilers cannot detect. It just promotes a false sense of security.

    The lack of threads saves you from a lot of hard to find race conditions that often don't show up till production. The lack of threads saves a lot of resources and overheads of context switching. It may require a programming style one is not used to.

    I hope to convince young professionals that although they can use JavaScript:

    In the browser.
    On the server.
    In their embedded apps for mobile devices (See Qt QML)
    As a scripting language in their game engines etc.
    In their micro-controller applications.

    They should be broad minded and aware of many available technologies, selecting the best one for the job at hand.

    They should be aware that when skimping on testing the compile time checks of a statically typed language do not help you much. There are many more logical errors you could have made that it cannot check. BUT then if you are testing properly those compile time checks are redundant.

    Logically compile time checks are not required.
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 07:56
    Loopy,

    Did you say "Java".

    Now there is a language that has no reason to exist.
  • Martin_HMartin_H Posts: 4,051
    edited 2013-12-07 08:00
    Loopy, because of its ubiquity, it's not as simple as avoiding it. If you do any Web development you'll have no choice, so it's worth learning to keep your options open. The problem is that JavaScript has often become people's first exposure to programming and it teaches bad habits as a virtue. So my goal here is to explain why these are bad habits.

    Continuing on my war against JavaScript.

    A popular JavaScript defense is to claim that the language is misunderstood. For example: http://javascript.crockford.com/javascript.html which is clever misdirection. But they often prove many criticism correct in the process by showing their work arounds to valid criticism.
  • Martin_HMartin_H Posts: 4,051
    edited 2013-12-07 08:11
    Heater. wrote: »
    Those 64 bits doubles can hold integers up to 53 bits. Not bad. It's not so hard to ensure they are integers at all times.

    You do realize how this is bad for any processor that doesn't have a built-in floating point unit? It's like the designer never thought of anything other than an x86.
    Heater. wrote: »
    Logically compile time checks are not required.

    If this is true then we should have spent the 90's programming in BLISS and FORTH over C and C++. Why did languages with type checking catch on over languages that didn't?

    Also threads and cogs are conceptual peers, as are events and interrupts. I like cogs over interrupts as they are more flexible.
  • jazzedjazzed Posts: 11,803
    edited 2013-12-07 08:52
    I thought this was a Javascript war thread :)

    According to forum rules, discussing other languages would be off topic, but I suppose that's up to Martin.

    My only real problem with Javascript is lack of uniformity. That is needing to code around "I.E." specific issues :)

    There are several ways to define a class. Which one works best? I usually use something like:
    function classname ()
    {
      var this.somevar;
      this.method1 = function(a,b) {}
      this.method2 = function(a,b) {}
      /* blah blah for constructor */
    }
    
    I've been using a class to "extend" the Image class. So I don't really get the no inheritance complaint.
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 09:13
    Martin_H,
    You do realize how this is bad for any processor that doesn't have a built-in floating point unit?
    I do indeed. Might be a bit slow. Rather like using a C compiler with 16 bit ints and or floats on a Z80 or AVR. Oh...people to do that. The speed may or may not matter, if it does use something else.

    By the way I have a micro-controller here, smaller than my thumb nail, that includes an FPU.
    It's like the designer never thought of anything other than an x86.
    I would not say he was stuck on x86. He just realized that having a multitude of types all different sizes and signedness was confusing and error prone. With that in mind big floats are the best choice.
    If this is true...
    I said "Logically compile time checks are not required." This is self evidently true. The proof goes like this:

    1) A type error, for example comparing a signed to an unsigned, is just an other error like any other, putting a minus where a plus should be for example.

    2) There are a huge, uncountably huge, number of such errors you can make in a program.

    3) Compile time type checking only catches a very small proportion of possible errors.

    4) You have to test to find all of the much bigger ocean of logical errors.

    5) Such testing would naturally catch all type errors if the compiler did not do the type checking.

    6) Ergo, if you do the testing you don't need the compile time checks.
    ...then we should have spent the 90's programming in BLISS and FORTH over C and C++
    God forbid. Clearly the huge volumes of security vulnerabilities that have been showing up for decades now in C/C++ and Java demonstrate that type checking does not help.
    Why did languages with type checking catch on over languages that didn't?
    Clearly everyone was as misled into a false sense of correctness as you:)

    Note: Ada is the only language I know that does type checking properly and it was soundly rejected by all those otherwise claiming to be in favour of compile time checks.
    Also threads and cogs are conceptual peers...
    No, they are not.

    A thread of execution is a software concept. A COG is a piece of hardware.

    In common parlance now a days a "thread" shares memory space with other threads. A "process" is operating in it's own memory space.

    What of a COG executing code and using data with it's registers only? That is isolated like a process. On the other hand if it is executing LMM code or using HUB for data it is more like a thread.
    ...as are events and interrupts
    No, they are not.

    An interrupt handler can potentially divert execution of your code at any point. This can be disastrous, causing race conditions, data corruption, and crashes, if you don't take steps to guard against that. All the perils of threaded code.

    There are no interrupts in event based software. Everything runs to completion and the the event queue is inspected to see if there is further work to do. In this way order is maintained. There are no possibilities for data races.

    (Yes, yes, there may be interrupts underneath an event based system. Though not necessarily as the XMOS micro-controllers demonstrate)
    I like cogs over interrupts as they are more flexible.
    Me too.
    A popular JavaScript defense is to claim that the language is misunderstood. For example...
    Given the misunderstandings of common computer science concepts you demonstrate in the above statements perhaps you should revisit the Crockford pages :)
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 09:23
    Jazzed,
    My only real problem with Javascript is lack of uniformity. That is needing to code around "I.E." specific issues.

    Let's not be confused. There is the language "JavaScript" as defined my ECMA standards. Then there is the environment that it runs in, the browsers, node.js, QML, Espruino, and so on.

    Turns out that Microsoft did an very good job of reverse engineering the JavaScript that Netscape put in Netscape Navigator. They implemented all the bugs and quirks of that original JS. When it came time to get the thing standardized at ECMA the Netscape guys wanted to fix many of the quirks. Those things Martin has mentioned above for example. Microsoft of course bullied the ECMA committee into standardizing JS as is, bugs and all.

    In short, the result of all that is that JavaScript, the language, is one of the most uniform languages among implementations from different vendors ever !

    It's the browser interface screw ups that cause all that apparent lack of uniformity.
  • KC_RobKC_Rob Posts: 465
    edited 2013-12-07 09:27
    Martin_H wrote: »
    This is the official JavaScript Religious War Thread.... But me first because I started it.
    It wasn't Heater? I don't believe it! LOL
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 09:29
    KC_Rob,

    Who me? I would never fire the first shot :)
  • Mike GMike G Posts: 2,702
    edited 2013-12-07 09:57
    I can come up with more, but I hope to convince young professionals that although they have use this language in a browser, please understand that it is completely broken and should be avoided otherwise.
    These language wars are always entertaining. Imagine for a second all the JavaScript executing in browsers at this very moment. Someone got paid for writing that stuff...

    I'm surprised you didn't mention variable scope and the affect of not using var.

    Not a fan of?
    <script>
    if (typeof(myvar) == 'undefined') 
    {
    	alert('Hello World!')
    }
    </script>
    
  • potatoheadpotatohead Posts: 10,254
    edited 2013-12-07 09:58
    []offers popcorn bowl to the crowd, settles in for a nice, entertaining read.]

    I learn something, or obtain a reference for later reading on just about every one of these BTW. All good, IMHO.
  • Martin_HMartin_H Posts: 4,051
    edited 2013-12-07 10:11
    KC_Rob wrote: »
    It wasn't Heater? I don't believe it! LOL

    I baited him in another thread because he mentioned that fetid swamp of a language.

    @Heater, not much time for a better response. Threads can be software concepts, but they can be implemented in hardware too. Threads have thread local storage (they keep the thread stack and thread local variables here) and access to a shared memory space. This is not unlike a cog's RAM and the Hub RAM. Intel's hyperthreaded processors can also be thought of as hardware support for threading because a single core implements some of the thread local storage and context switching.

    @all JavaScript's overloading the + operator should have been enough to consign it to the programming dustbin. But again it was saved by being the only game in town.

    Don't be lured by Heater's call for lack of type safety. It's the demon rum of programming!

    @Mike G, I got paid for writing some of that JavaScript. But I held my nose the whole time.
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 10:14
    Mike G,
    Imagine for a second all the JavaScript executing in browsers at this very moment

    Imagine, all those JavaScript engines were written in C++.

    What a glorious paradox.
  • jazzedjazzed Posts: 11,803
    edited 2013-12-07 10:14
    Heater. wrote: »
    Jazzed,


    Let's not be confused. There is the language "JavaScript" as defined my ECMA standards. Then there is the environment that it runs in, the browsers, node.js, QML, Espruino, and so on..
    I suppose event is not part of ecma then?

    Really I don't see the value of JavaScript without DOM. Maybe you can offer some useful examples where it excels over the alternatives.

    Cheers.
  • potatoheadpotatohead Posts: 10,254
    edited 2013-12-07 10:15
    lack of type safety. It's the demon rum of programming!

    ...unless one does their testing apparently.

    Was having a discussion about SPIN along those same lines. Came down to testing and doing that properly comes down to understanding the various cases and what is happening... which includes the type check by inference.

    Additionally, it can actually account for type abuses that will be just fine, tested to be so.

    A childhood friend works for a rather prominent software company. They use a LOT of Javascript, and he's head of testing. They also deploy on a ton of platforms, all of which requires testing too. He's in the job he's in, because he has asserted for basically ever that testing is what matters most. Authoring tests right alonside code is the optimal case in his view.

    Their product, incidentally is something people use to better optimize server side web applications and their interactions on various client platforms. Let's just say that product rarely fails. It can't given it's use case. Well, it can, but very bad things happen and people cry.

    Some additional advocacy on this would be enlightening. You've not yet met your burden Martin. :) Please do continue.

    I humbly request we don't kill this one off. So long as there are no serious bitings, I expect this discussion to have some value. Thanks.
  • Martin_HMartin_H Posts: 4,051
    edited 2013-12-07 10:33
    potatohead wrote: »
    ...unless one does their testing apparently.

    That's a common claim, but weak typing expands the number of test cases you need to write. I'd rather know up front that I blew it than have to write a test to find out. For the record, I tolerated Spin as the only game in town, but now that Propgcc is here I probably won't use it for new projects.
  • Martin_HMartin_H Posts: 4,051
    edited 2013-12-07 10:40
    @Heater, one more thing. Events in Windows are definitely not atomic, anytime you make a system call another event can be dispatched by the message loop before the system call returns. It was the bane of my existence for years! It's been a while, but can't a call back from setInterval interrupt another event handler? I seem to recall that happening on a single page JavaScript app I worked on a few years ago.
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 10:42
    Martin_H,
    I baited him in another thread...
    I always fall for it.
    Threads can be software concepts...
    Yes, yes. We know all that. I was just correcting your "threads and cogs are conceptual peers".
    JavaScript's overloading the + operator should have been enough to consign it to the programming dustbin.
    Quickly now, what do you get in memory if you put the following in a DAT section of a Spin program:
    something BYTE "Hello " + " world"
    
    Sounds like you are suggesting Spin be consigned to the dustbin.
    But again it was saved by being the only game in town.
    Not true.

    At the time the big thing in the browser was supposed to be Java, hence the Sun/Netscape hook up to make that happen. Turns out Java is hopeless. Also I seem to remember MS had "Active X controls" or whatever. And lets not forget FLASH. JavaScript won out over much competition.
    Don't be lured by Heater's call for lack of type safety.
    By all means use a "type safe" language. But don't forget that testing is the only way to know if your code works as desired.


    "Type safe" is mostly that is a misnomer anyway. In most languages they may be happy that all my variables are floats, say. But they will quite happily let me add kilograms to hertz. How dumb is that?


    If you want to be "type safe" be aware of what that means and then use Ada.
  • Martin_HMartin_H Posts: 4,051
    edited 2013-12-07 10:53
    @Heater, the perfect is the enemy of the good. Just because a language will allow you to confuse units (e. g. adding a pound to a kilogram) does not mean that saving you from adding an integer to a string is a bad thing, or having to write tests to prevent this is good. As far as Spin goes, I never warmed up to it and probably won't be using it anymore.

    PS the other things you mentioned are plug in technologies. JavaScript is integrated with the browser. Sun and MS fighting over Java is a big reason applet failed. Flash was actually pretty successful. ActiveX was an insane idea.
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 11:01
    Jazzed,
    I suppose event is not part of ecma then?
    Might be. What are we talking about here?
    Really I don't see the value of JavaScript without DOM
    JS has many uses without a DOM.

    Most of the JS I have ever written drives animated 2D and 3D data presentations in using webgl libraries. I try and keep as far away from the DOM as possible.

    JS is used to good effect on the server with node.js. No DOM there.

    JS is used to create user interfaces using Qt for mobile applications, see Qt Quick.

    JS is used to program micro-controllers. See Espruino and Tessel.

    JS is used in game engines.

    It's pretty much everywhere !
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 11:27
    Potatohead,
    Came down to testing and doing that properly comes down to understanding the various cases and what is happening... which includes the type check by inference.
    Yes exactly. Thank God, some one who understands :)


    Let's take an example, here is some code that needs testing (pseudo code):
    if a = b and c <= d
       result := 1
    else
       result := 2
    end if
    
    How many tests do we need to be sure this works as planned ?


    Let's try and map out some example tests:
    a   b   c   d   expected result
    2   2   MIN 10  1               ; Check a <= d comparison.
    2   2   8   10  1
    2   2   9   10  1
    2   2   10  10  1
    2   2   11  10  2
    2   2   MAX 10  2
    
    
    MIN 2   10  10  2               ; Check A = b comparison. 
    1   2   10  10  2
    2   2   10  10  1
    3   2   10  10  2
    MAX 2   10  10  2
    
    
    Note: MIN/MAX are the minimum and maximum values for the variables.
    Note: You can probably see some more tests required here. 
    


    Clearly if that code is to be known to behave as you would like it has to pass those tests, and more. Also clearly if it passes those tests it has also done the type checking for you. Comparing signed and unsigned would fail for example.
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 11:40
    Martin_ H
    ...but weak typing expands the number of test cases you need to write.
    Not really. Many places I have worked (avionics, military) they would not trust the compiler to be generating correct code so we had to do all that testing.
    As I said given the number of security vulnerabilities we have see all around in recent decades it seems that level of testing is needed anyway.

    Also, "type safe" languages are not. Remember the "kilos + hertz" thing. Except possibly Ada.
    Events in Windows are definitely not atomic, anytime you make a system call another event can be dispatched by the message loop before the system call returns.
    This is way off topic. What you are saying is that the Windows evented API is broken. No surprise there :)
    ...can't a call back from setInterval interrupt another event handler?
    No. Not unless the implementation is broken.

    All events from timers, or I/O or whatever are placed on an event queue. The event queue is only checked when the event loop has completed whatever code it had to run due to the previous event.
  • Martin_HMartin_H Posts: 4,051
    edited 2013-12-07 11:43
    Heater. wrote: »
    It's pretty much everywhere !

    Which is a scary thought. Fortunately I expect people will get burned by it and realize their mistake.
  • potatoheadpotatohead Posts: 10,254
    edited 2013-12-07 11:56
    That's a common claim, but weak typing expands the number of test cases you need to write.

    Really?

    The tests are to insure it works. What if the language itself has some trouble, or gets an update? Failure to write inclusive tests leave those regressions wide open. Happens every day.
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 11:57
    Martin,

    Perhaps being a browser plugin is a pain and a hindrance to acceptance. Well, that's tough for them. I'm here to debate the language and dynamic vs static languages.
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 12:00
    Martin,
    Fortunately I expect people will get burned by it and realize their mistake.
    Chuckle, used to think thoughts like that about BASIC when it was all the rage on 8 bit machines. BASIC went on to be huge anyway.
  • Martin_HMartin_H Posts: 4,051
    edited 2013-12-07 12:01
    Heater. wrote: »
    Not really. Many places I have worked (avionics, military) they would not trust the compiler to be generating correct code so we had to do all that testing.
    ...
    No. Not unless the implementation is broken.

    In many industries you would never get the time in your schedule to verify the compiler. You barely get the time to do decent edge case unit tests.

    My setInterval bug was in 2009 and we had to support IE6. So broken was a distinct possibility.
  • Martin_HMartin_H Posts: 4,051
    edited 2013-12-07 12:08
    potatohead wrote: »
    The tests are to insure it works. What if the language itself has some trouble, or gets an update? Failure to write inclusive tests leave those regressions wide open. Happens every day.

    You never update your language or runtime in the middle of a project. Only the beginning so that all the regression testing and ad hoc testing isn't invalidated.
  • Heater.Heater. Posts: 21,230
    edited 2013-12-07 12:08
    Martin,
    In many industries you would never get the time in your schedule to verify the compiler.
    That is very true.

    It's also why there are all those security vulnerabilities and millions of people complaining about buggy software every day.

    It's also why people turn to dynamic languages like JS, PHP, Python, etc etc etc. So that they can get stuff out the door in time with out fighting with the compiler all day.

    (Errr, that is not one of my arguments for JS by the way)
Sign In or Register to comment.