Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Improving My Javascript Questions

    I recently read a couple blogs that listed some points for writing better Javascript code.

    Several points applied to the code I write, so they caught my eye.
    For example,

    Use === instead of ==
    and

    Use [] Instead of New Array()
    All of my programs are for numerical computations.
    Input comes from a textarea box, it is converted to floats, then it is put in arrays, compared, manipulated, etc.

    For example,

    Code:
    rawArray = rawtextareaString.split(/\s+/g);
     . . .
    tempx = parseFloat(rawArray[k]);
    . . .
    sqAMatrix[i][j] = tempx;
    . . .
    Before I rush out and revise all my code to incorporate these suggestions, are there are concerns I should be aware of? Gotchas? Compatibility issues? Should I even bother?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You might want to rethink using floats as they make the number the nearest binary equivalent and so only give approximations of the actual numbers - makes testing for equal to a given value impossible.

    If you work with integers instead (by multiplying the input by whatever power of 10 corresponds to the number of of decimal places you want and dividing by that again at the end) then you can maintain the numbers exactly instead of only approximately.

    Ignore this suggestion if you are working with very large and or very small numbers (eg. 1e15 or 1e-16) as there you will not be able to do better than approximate the numbers as JavaScript is only accurate to about 14 digits. Also the only way to get a number such as 1e100 into a variable is to use parseFloat to convert it from a string as you can't enter it directly because of the exponential.
    Last edited by felgall; 03-10-2012 at 03:31 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    You might want to rethink using floats . . .
    Unfortunately, I cannot avoid using floats; all my programs are for math, science, engineering, etc. applications. For example, computing the roots of a transcendental function, inverting a matrix, computing the eigenvalues of a matrix, etc.

    Although, when it comes to comparing two floating point numbers, and getting results as accurate as possible, I have tried to be as anal as possible to wring as much accuracy out of my Javascript programs. For example, when you compare two floats, I suppose you could compute DBL_EPSILON, and then do something like the following:

    instead of

    Code:
    if (number1 == number2)
       do something;
    something like

    Code:
    if (Math.abs(number1 - number2) <= DBL_EPSILON) // the numbers are effectively equal
       do something;
    In any case, returning to my original question, all my code uses the old practice of using == and defining arrays with the new operator. Now I am considering going back and editing my code to the new (better?) way. Is it worthwhile to go back and make these changes?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by DavidB View Post
    something like

    Code:
    if (Math.abs(number1 - number2) <= DBL_EPSILON) // the numbers are effectively equal
       do something;
    Yes that's the way to do it - that is the sort of application where using parseFloat is appropriate.

    Quote Originally Posted by DavidB View Post
    In any case, returning to my original question, all my code uses the old practice of using == and defining arrays with the new operator. Now I am considering going back and editing my code to the new (better?) way. Is it worthwhile to go back and make these changes?
    I wouldn't bother going back and changing working code written that way unless it used other even more outdated techniques that needed to be replaced or the code needed changing for some other reason.

    Use the most modern way of coding for the new code you write and update older code when you need to change it or it becomes so far out of date that it becomes obvious because it can't do all the things people would expect (such as document.write) or presents security issues (such as eval) or clashes with new scripts to be added to the page because it wasn't written to be completely unobtrusive.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •