...

View Full Version : Improving My Javascript Questions



DavidB
03-10-2012, 02:04 AM
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,


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?

felgall
03-10-2012, 02:27 AM
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.

DavidB
03-10-2012, 06:47 PM
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


if (number1 == number2)
do something;


something like


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?

felgall
03-10-2012, 09:01 PM
something like


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.


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum