Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Oh JavaScript, you silly goose.

Oh JavaScript, you silly goose.

Scheduled Pinned Locked Moved The Lounge
javascriptdatabasequestion
40 Posts 14 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeremy Falcon
    wrote on last edited by
    #1

    After years and years of using JavaScript, only now do I find that checking for zero on the denominator when dividing is a complete waste of time since JavaScript handles it gracefully without crashing.

    console.log(0 / 0); // returns NaN
    console.log(1 / 0); // returns Infinity

    // let's get fancy
    var divide = (...args) => args.reduce((a, b) => a / b);

    console.log(divide(0, 0)); // still returns NaN
    console.log(divide(1, 0)); // still returns Infinity

    // if I want to save the sucker in a database
    var result = divide(42, 0);
    saveMeSomehow.result = isNaN(result) || !isFinite(result) ? 0 : result;

    These young kids these days just don't know how spoiled they are. Note: I don't think the syntax highlighting supports ES6 yet, so yay I broke the Internet.

    Jeremy Falcon

    L K N Richard DeemingR C 9 Replies Last reply
    0
    • J Jeremy Falcon

      After years and years of using JavaScript, only now do I find that checking for zero on the denominator when dividing is a complete waste of time since JavaScript handles it gracefully without crashing.

      console.log(0 / 0); // returns NaN
      console.log(1 / 0); // returns Infinity

      // let's get fancy
      var divide = (...args) => args.reduce((a, b) => a / b);

      console.log(divide(0, 0)); // still returns NaN
      console.log(divide(1, 0)); // still returns Infinity

      // if I want to save the sucker in a database
      var result = divide(42, 0);
      saveMeSomehow.result = isNaN(result) || !isFinite(result) ? 0 : result;

      These young kids these days just don't know how spoiled they are. Note: I don't think the syntax highlighting supports ES6 yet, so yay I broke the Internet.

      Jeremy Falcon

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      This way of sweeping all kinds of errors under the rug is one of the many reasons why I will never voluntarily use it for any serious work.

      The language is JavaScript. that of Mordor, which I will not utter here
      This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
      "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

      J 1 Reply Last reply
      0
      • J Jeremy Falcon

        After years and years of using JavaScript, only now do I find that checking for zero on the denominator when dividing is a complete waste of time since JavaScript handles it gracefully without crashing.

        console.log(0 / 0); // returns NaN
        console.log(1 / 0); // returns Infinity

        // let's get fancy
        var divide = (...args) => args.reduce((a, b) => a / b);

        console.log(divide(0, 0)); // still returns NaN
        console.log(divide(1, 0)); // still returns Infinity

        // if I want to save the sucker in a database
        var result = divide(42, 0);
        saveMeSomehow.result = isNaN(result) || !isFinite(result) ? 0 : result;

        These young kids these days just don't know how spoiled they are. Note: I don't think the syntax highlighting supports ES6 yet, so yay I broke the Internet.

        Jeremy Falcon

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You didn't know about NaN yet? Or infinity? Or that JS uses elephanting floats all over the place? It gets better, 1/0 is positive infinity but 1/-0 is negative infinity, even though 0 and -0 compare equal to each other.

        J Kornfeld Eliyahu PeterK 3 Replies Last reply
        0
        • L Lost User

          This way of sweeping all kinds of errors under the rug is one of the many reasons why I will never voluntarily use it for any serious work.

          The language is JavaScript. that of Mordor, which I will not utter here
          This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
          "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

          J Offline
          J Offline
          Jeremy Falcon
          wrote on last edited by
          #4

          It doesn't just sweep it under the rug. It returns Infinity rather than just crashing. It seems like a much better solution than bringing down the entire system.

          Jeremy Falcon

          L P M 3 Replies Last reply
          0
          • L Lost User

            You didn't know about NaN yet? Or infinity? Or that JS uses elephanting floats all over the place? It gets better, 1/0 is positive infinity but 1/-0 is negative infinity, even though 0 and -0 compare equal to each other.

            J Offline
            J Offline
            Jeremy Falcon
            wrote on last edited by
            #5

            Nice!!!

            Jeremy Falcon

            1 Reply Last reply
            0
            • L Lost User

              You didn't know about NaN yet? Or infinity? Or that JS uses elephanting floats all over the place? It gets better, 1/0 is positive infinity but 1/-0 is negative infinity, even though 0 and -0 compare equal to each other.

              J Offline
              J Offline
              Jeremy Falcon
              wrote on last edited by
              #6

              harold aptroot wrote:

              You didn't know about NaN yet? Or infinity? Or that JS uses elephanting floats all over the place?

              Everyone knows about NaN, what I didn't know was that I could safely divide by zero without crashing the JavaScript engine / interpreter.

              Jeremy Falcon

              1 Reply Last reply
              0
              • J Jeremy Falcon

                It doesn't just sweep it under the rug. It returns Infinity rather than just crashing. It seems like a much better solution than bringing down the entire system.

                Jeremy Falcon

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Ok, but I must admit that I still do not like it. This way you always have to test the results which bloats up the code. First, it's an avoidable error and, assuming it comes to my attention, I will find out why the denominater can become zero and then eliminate the problem once and for all. So what's won by bravely keeping going until the error manifests itself somplace else? You will just have to spend more time tracing it back to its origin and then essentially doing the same work to correct it.

                The language is JavaScript. that of Mordor, which I will not utter here
                This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                J B 3 Replies Last reply
                0
                • L Lost User

                  Ok, but I must admit that I still do not like it. This way you always have to test the results which bloats up the code. First, it's an avoidable error and, assuming it comes to my attention, I will find out why the denominater can become zero and then eliminate the problem once and for all. So what's won by bravely keeping going until the error manifests itself somplace else? You will just have to spend more time tracing it back to its origin and then essentially doing the same work to correct it.

                  The language is JavaScript. that of Mordor, which I will not utter here
                  This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                  "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                  J Offline
                  J Offline
                  Jeremy Falcon
                  wrote on last edited by
                  #8

                  Oh I totally agree with you on those points. I'm a very defensive programmer so I'd always validate my inputs. To the point my coworkers think I'm nuts for being so verbose. I've been doing some hardcore JS development in the past couple years. I'm just delighted to know that this won't crash the system anymore, which is a step in the right direction. I'll always validate my inputs. It's what Jesus would do.

                  Jeremy Falcon

                  M 1 Reply Last reply
                  0
                  • L Lost User

                    Ok, but I must admit that I still do not like it. This way you always have to test the results which bloats up the code. First, it's an avoidable error and, assuming it comes to my attention, I will find out why the denominater can become zero and then eliminate the problem once and for all. So what's won by bravely keeping going until the error manifests itself somplace else? You will just have to spend more time tracing it back to its origin and then essentially doing the same work to correct it.

                    The language is JavaScript. that of Mordor, which I will not utter here
                    This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                    "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                    J Offline
                    J Offline
                    Jeremy Falcon
                    wrote on last edited by
                    #9

                    After rereading my orginal post, I can totally see how you'd think I'd no longer validate input. I'd change the text but it's much more fun letting readers be confused.

                    Jeremy Falcon

                    1 Reply Last reply
                    0
                    • J Jeremy Falcon

                      It doesn't just sweep it under the rug. It returns Infinity rather than just crashing. It seems like a much better solution than bringing down the entire system.

                      Jeremy Falcon

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      No; keeping the application from crashing is the application developer's job, not the language or library developer's job.

                      J B 2 Replies Last reply
                      0
                      • J Jeremy Falcon

                        Oh I totally agree with you on those points. I'm a very defensive programmer so I'd always validate my inputs. To the point my coworkers think I'm nuts for being so verbose. I've been doing some hardcore JS development in the past couple years. I'm just delighted to know that this won't crash the system anymore, which is a step in the right direction. I'll always validate my inputs. It's what Jesus would do.

                        Jeremy Falcon

                        M Offline
                        M Offline
                        Mycroft Holmes
                        wrote on last edited by
                        #11

                        Jeremy Falcon wrote:

                        I'm a very defensive programmer so I'd always validate my inputs

                        And you wonder why you are just now finding out the divide by zero handling in JS :laugh: .

                        Never underestimate the power of human stupidity RAH

                        L J 2 Replies Last reply
                        0
                        • M Mycroft Holmes

                          Jeremy Falcon wrote:

                          I'm a very defensive programmer so I'd always validate my inputs

                          And you wonder why you are just now finding out the divide by zero handling in JS :laugh: .

                          Never underestimate the power of human stupidity RAH

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Are you back in Oz over Christmas. If so we better catch up for a beer.

                          Michael Martin Australia "I controlled my laughter and simple said "No,I am very busy,so I can't write any code for you". The moment they heard this all the smiling face turned into a sad looking face and one of them farted. So I had to leave the place as soon as possible." - Mr.Prakash One Fine Saturday. 24/04/2004

                          M 1 Reply Last reply
                          0
                          • J Jeremy Falcon

                            It doesn't just sweep it under the rug. It returns Infinity rather than just crashing. It seems like a much better solution than bringing down the entire system.

                            Jeremy Falcon

                            M Offline
                            M Offline
                            Marc Clifton
                            wrote on last edited by
                            #13

                            Until you write stock trading software in Javascript and you end up buying an infinity of shares because somewhere along the line, some value in some database was 0 that you used in a denominator to figure out how many shares to buy. ;) Marc

                            Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

                            J 1 Reply Last reply
                            0
                            • L Lost User

                              Are you back in Oz over Christmas. If so we better catch up for a beer.

                              Michael Martin Australia "I controlled my laughter and simple said "No,I am very busy,so I can't write any code for you". The moment they heard this all the smiling face turned into a sad looking face and one of them farted. So I had to leave the place as soon as possible." - Mr.Prakash One Fine Saturday. 24/04/2004

                              M Offline
                              M Offline
                              Mycroft Holmes
                              wrote on last edited by
                              #14

                              Cambodia, going to see the beaches there. Oz is only in cairns these days, very rarely get to Sydney.

                              Never underestimate the power of human stupidity RAH

                              1 Reply Last reply
                              0
                              • M Marc Clifton

                                Until you write stock trading software in Javascript and you end up buying an infinity of shares because somewhere along the line, some value in some database was 0 that you used in a denominator to figure out how many shares to buy. ;) Marc

                                Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

                                J Offline
                                J Offline
                                Jeremy Falcon
                                wrote on last edited by
                                #15

                                That's just example code man, cut me some slack. It's Christmas!

                                Jeremy Falcon

                                1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  No; keeping the application from crashing is the application developer's job, not the language or library developer's job.

                                  J Offline
                                  J Offline
                                  Jeremy Falcon
                                  wrote on last edited by
                                  #16

                                  I take you don't do much hardcore JavaScript development. I'd rather check for values than have the entire web page stop working with no way to recover because the engine went bye bye.

                                  Jeremy Falcon

                                  P 1 Reply Last reply
                                  0
                                  • M Mycroft Holmes

                                    Jeremy Falcon wrote:

                                    I'm a very defensive programmer so I'd always validate my inputs

                                    And you wonder why you are just now finding out the divide by zero handling in JS :laugh: .

                                    Never underestimate the power of human stupidity RAH

                                    J Offline
                                    J Offline
                                    Jeremy Falcon
                                    wrote on last edited by
                                    #17

                                    Good point.

                                    Jeremy Falcon

                                    1 Reply Last reply
                                    0
                                    • J Jeremy Falcon

                                      I take you don't do much hardcore JavaScript development. I'd rather check for values than have the entire web page stop working with no way to recover because the engine went bye bye.

                                      Jeremy Falcon

                                      P Offline
                                      P Offline
                                      PIEBALDconsult
                                      wrote on last edited by
                                      #18

                                      I don't do any of that Web crap; I do real software development.

                                      J 1 Reply Last reply
                                      0
                                      • P PIEBALDconsult

                                        I don't do any of that Web crap; I do real software development.

                                        J Offline
                                        J Offline
                                        Jeremy Falcon
                                        wrote on last edited by
                                        #19

                                        Would you like a lollipop?

                                        Jeremy Falcon

                                        1 Reply Last reply
                                        0
                                        • L Lost User

                                          You didn't know about NaN yet? Or infinity? Or that JS uses elephanting floats all over the place? It gets better, 1/0 is positive infinity but 1/-0 is negative infinity, even though 0 and -0 compare equal to each other.

                                          Kornfeld Eliyahu PeterK Offline
                                          Kornfeld Eliyahu PeterK Offline
                                          Kornfeld Eliyahu Peter
                                          wrote on last edited by
                                          #20

                                          That positive 0 and negative 0, of course has nothing to do with JS... It comes from the IEEE 754 standard, and you can find it in C# implementation too... Exception handling is very expensive in terms of computer resources, so we already used to validate user input...In this case JS follows to the letter the standard... https://en.wikipedia.org/wiki/IEEE_floating_point[^]

                                          Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

                                          "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                                          L 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups