473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help refactoring functions...

cjl
Hey all:

I have the following (ugly) code:

function preload()
{
if (cr_series.length >1) preloadCR();
if (ct_series.length >1) preloadCT();
if (mr_series.length >1) preloadMR();
if (us_series.length >1) preloadUS();
if (xr_series.length >1) preloadXR();
}

function preloadCR()
{
cr_images = new Array();
for (var loop = 0; loop <= (cr_series.length-2); loop++)
{
cr_images[loop] = new Array();

for (var i = 0;i<4;i++)
{
cr_images[0][i] = new Image();
cr_images[0][i].src = "images/cr" + (loop+1) + "_" + (i+1) + ".jpg";
}
}
}

function preloadCT()
{
ct_images = new Array();
for (var loop = 0; loop <= (ct_series.length-2); loop++)
{
ct_images[loop] = new Array();

for (var i = 0;i<4;i++)
{
ct_images[0][i] = new Image();
ct_images[0][i].src = "images/ct" + (loop+1) + "_" + (i+1) + ".jpg";
}
}
}
As you can see, the two preload functions (and the others I didn't
paste here) are all but identical, except for the names of the arrays
created and used.

I am a beginner, and having trouble writing a generic function that
could do the same thing, but be called with a paramater:

if (cr_series.length > 1) preload('cr');

I don't know how to take the passed paramated and use it on the 'left'
side of an expression to dynamically create a variable name, etc....
can anyone point me in the right direction?

thanks,
cjl

Jul 23 '05 #1
21 1334
cjl wrote:
Hey all:

I have the following (ugly) code:

function preload()
{
if (cr_series.length >1) preloadCR();
if (ct_series.length >1) preloadCT();
if (mr_series.length >1) preloadMR();
if (us_series.length >1) preloadUS();
if (xr_series.length >1) preloadXR();
}

function preloadCR()
{
cr_images = new Array();
for (var loop = 0; loop <= (cr_series.length-2); loop++)
{
cr_images[loop] = new Array();

for (var i = 0;i<4;i++)
{
cr_images[0][i] = new Image();
cr_images[0][i].src = "images/cr" + (loop+1) + "_" + (i+1) + ".jpg";
}
}
}

function preloadCT()
{
ct_images = new Array();
for (var loop = 0; loop <= (ct_series.length-2); loop++)
{
ct_images[loop] = new Array();

for (var i = 0;i<4;i++)
{
ct_images[0][i] = new Image();
ct_images[0][i].src = "images/ct" + (loop+1) + "_" + (i+1) + ".jpg";
}
}
}
As you can see, the two preload functions (and the others I didn't
paste here) are all but identical, except for the names of the arrays
created and used.

I am a beginner, and having trouble writing a generic function that
could do the same thing, but be called with a paramater:

if (cr_series.length > 1) preload('cr');
That's the right idea...

When posting code, use indents of 2 or 4 spaces, not tabs, and manually
wrap lines at about 70 characters to prevent auto-wrapping. It makes
life for those who would reply much easier.

I don't know how to take the passed paramated and use it on the 'left'
side of an expression to dynamically create a variable name, etc....
can anyone point me in the right direction?


Call a function that creates the array, then return a reference to it to
the original function (I presume you want to make these global). Do you
want to load the images into 'cr_series', 'ct_series', etc. or a new
variable?

I'll presume that you created 'cr_series' etc. as a global variables
elsewhere and that you want to load the images into them. It seems that
the parameter you pass is the directory name where the images are
stored, so I've passed it as a string.

I'm also a bit bemused about the '+1' bit. It's much easier to make
everything zero-indexed, but if you can't change that I guess you just
have to work with it (but it makes the code more obtuse and more
difficult to maintain).
function preload() {
if (cr_series.length > 1) cr_series = loadArray('cr');
if (ct_series.length > 1) ct_series = loadArray('ct');
// and so on...
}

function loadArray( parm ) {
var A = [];
for (var loop=0, len=(cr_series.length-2); loop<=len; loop++) {
A[loop] = [];

for (var i=0; i<4; i++ ) {
A[0][i] = new Image();
A[0][i].src = 'images/' + parm + (loop+1)
+ '_' + (i+1) + '.jpg';
}
}
return A;
}

A is local to the 'loadArray()' function. It creates the array, then
returns a reference to it to the calling function (preload). Because a
reference persists, the array stays alive.

Here is a more concise version of the loadArray function:

function loadArray( d ) {
var A = [];
var j, i = cr_series.length; // no '- 2' ... see below

while ( --i ) {
A[i] = [];
j = 5; // not 4 ... see below

while ( --j ) {
A[i][j] = new Image();
A[i][j].src = 'images/' + d + i + '_' + j + '.jpg';
}
}
return A;
}

I've used a pre-decrement operator ( --i ) so that i will have values
inside the while loop of (whatever-it-is-set-at - 1) down to 1 (when it
hits zero the while exits *before* doing the loop content). Same for j.
Saves all the +/-1 stuff.

You may want to pass the value for j as a parameter too, hard-coding '5'
inside the loadArray function reduces it's re-usability.

Untested of course, but it should work (OK, maybe a little debugging
required...).
--
Rob
Jul 23 '05 #2
cjl
Rob:

Thanks for the quick reply. I will follow your advice for posting code
in the future.

You wrote:
I'll presume that you created 'cr_series' etc. as a global variables
elsewhere and that you want to load the images into them. It seems that
the parameter you pass is the directory name where the images are
stored, so I've passed it as a string.
You presumed correctly. There is a subdirectory named 'images'. In it
the user will place the relevant images, adhering to a naming
convention. If there are for example two series of CR images and one
series of CT images, the directory might contain "cr1_1.jpg, cr1_2.jpg,
cr1_3.jpg,cr1_4.jpg, cr2_1.jpg, cr2_2.jpg, ct1_1.jpg, ct1_2.jpg,
ct1_3.jpg, ct1_4.jpg, ct1_5.jpg" etc, you get the picture. Then the
user edits the arrays in a seperate js file, such that in this example
there would be:

cr_series = [4,2,0];
ct_series = [5,0];
I'm also a bit bemused about the '+1' bit. It's much easier to make
everything zero-indexed, but if you can't change that I guess you just
have to work with it (but it makes the code more obtuse and more
difficult to maintain).
Some of the users will not understand zero-index, and the naming
convention for the images will suffer.

function preload() {
if (cr_series.length > 1) cr_series = loadArray('cr');
if (ct_series.length > 1) ct_series = loadArray('ct');
// and so on...
}

function loadArray( parm ) {
var A = [];
for (var loop=0, len=(cr_series.length-2); loop<=len; loop++) {
A[loop] = [];

for (var i=0; i<4; i++ ) {
A[0][i] = new Image();
A[0][i].src = 'images/' + parm + (loop+1)
+ '_' + (i+1) + '.jpg';
}
}
return A;
}


I think there is a problem with this code, which is exactly the
question I was trying to ask in my original post.

Your code has cr_series.length "hard-coded" into the loadArray
function. This array actually needs to change depending on the
paramater passed to the function. If I want to preload CR images, then
cr_series.length is fine, but your example would not work for CT
images.

Let me try to explain the generic function I would like to write. If
the passed parameter is "cr", then it should create an
(multidimensional)array cr_images like my original code does. If I pass
"ct" then it should created a (multidimensional) array ct_images.
These arrays will then be accessed later in the code.

something like:
preload(foo)
{
'foo'_array = code goes here;
}

Is this possible?

thanks again,
cjl

Jul 23 '05 #3
cjl
Hey guys;

Another thought just occured to me...
is eval() what I am looking for to dynamically create variables, etc.?

-CJL

Jul 23 '05 #4
cjl
Hey all:

I've got an extremely ugly refactored function working, using eval()

function preload()
{
if (cr_series.length >1) preloadIt('cr');
if (ct_series.length >1) preloadIt('ct');
}

function preloadIt(what)
{
eval("" + what + "_images = new Array();");

for (var loop = 0; loop <= eval("" + what + "_series.length-2");
loop++)
{
eval("" + what + "_images[loop] = new Array();");

for (var i =0; i < eval("" + what + "_series[loop]"); i++)
{
eval("" + what + "_images[0][i] = new Image();");
eval("" + what + "_images[0][i].src = \"images/" + what + "\" +
(loop+1) + \"_\" + (i+1) + \".jpg\";");
}
}
}

whew. I guess it keeps me from repeating myself, but I bet if I look at
this code in the morning I won't remember what I did.

Is there another way to accomplish this, without eval() ?

-CJL

Jul 23 '05 #5
Lee
cjl said:

Rob:

Thanks for the quick reply. I will follow your advice for posting code
in the future.

You wrote:
I'll presume that you created 'cr_series' etc. as a global variables
elsewhere and that you want to load the images into them. It seems that
the parameter you pass is the directory name where the images are
stored, so I've passed it as a string.


You presumed correctly. There is a subdirectory named 'images'. In it
the user will place the relevant images, adhering to a naming
convention. If there are for example two series of CR images and one
series of CT images, the directory might contain "cr1_1.jpg, cr1_2.jpg,
cr1_3.jpg,cr1_4.jpg, cr2_1.jpg, cr2_2.jpg, ct1_1.jpg, ct1_2.jpg,
ct1_3.jpg, ct1_4.jpg, ct1_5.jpg" etc, you get the picture. Then the
user edits the arrays in a seperate js file, such that in this example
there would be:

cr_series = [4,2,0];
ct_series = [5,0];
I'm also a bit bemused about the '+1' bit. It's much easier to make
everything zero-indexed, but if you can't change that I guess you just
have to work with it (but it makes the code more obtuse and more
difficult to maintain).


Some of the users will not understand zero-index, and the naming
convention for the images will suffer.

function preload() {
if (cr_series.length > 1) cr_series = loadArray('cr');
if (ct_series.length > 1) ct_series = loadArray('ct');
// and so on...
}

function loadArray( parm ) {
var A = [];
for (var loop=0, len=(cr_series.length-2); loop<=len; loop++) {
A[loop] = [];

for (var i=0; i<4; i++ ) {
A[0][i] = new Image();
A[0][i].src = 'images/' + parm + (loop+1)
+ '_' + (i+1) + '.jpg';
}
}
return A;
}


I think there is a problem with this code, which is exactly the
question I was trying to ask in my original post.

Your code has cr_series.length "hard-coded" into the loadArray
function. This array actually needs to change depending on the
paramater passed to the function.


I think that was an oversight in his code.
Simply change "cr_series.length" to "window[parm+'_series'].length".

Global variables can be referenced as attributes of the window Object
using this "bracket" notation.

Jul 23 '05 #6
cjl
Lee:

What is the difference, if any, between using eval() and using the
window Object "bracket" notation you described?

-CJL

Jul 23 '05 #7
Lee
cjl said:

Lee:

What is the difference, if any, between using eval() and using the
window Object "bracket" notation you described?


The bracket notation is more efficient and is also easier to debug.
Using eval() requires invoking the Javascript compiler for each use.

Jul 23 '05 #8
cjl wrote:
Rob:
[...]
cr_series = [4,2,0];
ct_series = [5,0];
Consider getting rid of the trailing '0' element, it makes life more
difficult.
I'm also a bit bemused about the '+1' bit. It's much easier to make
everything zero-indexed, but if you can't change that I guess you just
have to work with it (but it makes the code more obtuse and more
difficult to maintain).

Some of the users will not understand zero-index, and the naming
convention for the images will suffer.


C'est la vie.
function preload() { [...] }

I think there is a problem with this code, which is exactly the
question I was trying to ask in my original post.

Your code has cr_series.length "hard-coded" into the loadArray
function. This array actually needs to change depending on the
paramater passed to the function. If I want to preload CR images, then
cr_series.length is fine, but your example would not work for CT
images.


Ok, so the number of series is the length of the array, and the number
of images in each series is given by the values of the elements. So
pass the array and extract the values from it.
Let me try to explain the generic function I would like to write. If
the passed parameter is "cr", then it should create an
(multidimensional)array cr_images like my original code does. If I pass
"ct" then it should created a (multidimensional) array ct_images.
These arrays will then be accessed later in the code.

something like:
preload(foo)
{
'foo'_array = code goes here;
}

Is this possible?


Start from where you declare cr_series (dropping the trailing '0'):

cr_series = [4,2]; // declared as a global

Now call preload():

function preload() {
if ( cr_series.length > 1 ) cr_images = loadArray( cr_series, 'cr' );
...
}

function loadArray( x, d ) {

// x is reference to cr_series array, d to directory name
// The number of series to load (2) is the length of x
var j, i = x.length;
var A = [];
while ( i-- )

// the number of images to load (j) is in the array
j = x[i];

// Make the ith element of A an array
A[i] = [];

// load the images
while ( j-- ) {
A[i][j] = new Image();
A[i][j].src = 'images/' + d + (i+1) + '_' + (j+1) + '.jpg';
}
}
return A;
}

And that's it.

cr_images[0][0] = 'images/cr1_1.jpg'
cr_images[0][1] = 'images/cr1_2.jpg'
cr_images[0][2] = 'images/cr1_3.jpg'
cr_images[0][3] = 'images/cr1_4.jpg'
cr_images[1][0] = 'images/cr2_1.jpg'
cr_images[1][1] = 'images/cr2_2.jpg'
Here's a test script:

<script type="text/javascript">

var cr_series = [4, 2];

function preload() {
if ( cr_series.length > 1 ) cr_images = loadArray( cr_series, 'cr' );
}

function loadArray( x, d ) {
var j, i = x.length;
var A = [];
while ( i-- ){
j = x[i];
A[i] = [];
while ( j-- ) {
A[i][j] = new Image();
A[i][j].src = 'images/' + d + (i+1) + '_' + (j+1) + '.jpg';
}
}
return A;
}

// Call preload
preload();

// Have a look at cr_images
var msg = [];
for ( var m=0; m<cr_images.length; m++){
for ( var p=0; p<cr_images[m].length; p++ ){
msg.push( cr_images[m][p].src );
}
}
alert( 'cr_images after loading:\n\n' + msg.join('\n') );

</script>
Incidentally, if you (or your visitors) have Firefox configured to not
allow scripts to change images, you can't set the src attribute of the
new image objects, they are blank. No error message or warning is given.
--
Rob
Jul 23 '05 #9
VK
> Global variables can be referenced as attributes
of the window Object using this "bracket" notation.


Or in more global approach (using global or local):

function bufferedReader(prefix) {
var myName = prefix + '_series';
var buffer = new Object();
buffer[myName] = new Array();
...
}

Jul 23 '05 #10
cjl wrote:
Hey all:

[...]

Please, kill it before it breeds... :-x

--
Rob
Jul 23 '05 #11
Lee wrote:
cjl said: [...] I think that was an oversight in his code.
Simply change "cr_series.length" to "window[parm+'_series'].length".

Global variables can be referenced as attributes of the window Object
using this "bracket" notation.


Hey, the penny finally dropped:

<script type="text/javascript">

var cr_series = [4, 2];
var ct_series = [2, 3];

function preload() {
for ( parm in window ) {
if ( parm.match('_series') ) {
loadArray( parm.replace('_series','') )
}
}
}

function loadArray( d ) {
var x = window[d+'_series'];
var j, i = x.length;
var A = [];
while ( i-- ){
j = x[i];
A[i] = [];
while ( j-- ) {
A[i][j] = new Image();
A[i][j].src = 'images/' + d + (i+1) + '_' + (j+1) + '.jpg';
}
}
window[d+'_images'] = A;
}

// Call preload
preload();

function showArray( y ) {
var x = window[y];
var msg = [];
for ( var m=0; m<x.length; m++){
for ( var p=0; p<x[m].length; p++ ){
msg.push( x[m][p].src );
}
}
alert( y + ':\n\n' + msg.join('\n') );
}

// Have a look at cr_images
for ( parm in window ) {
if ( parm.match('_images') ) showArray( parm );
}

</script>
--
Rob
Jul 23 '05 #12
cjl
Hey all:

Thank you all. I obviously have a lot to learn.

Rob, your last example was sweet. Let me see if I understand it:

//declare my 'user editable' arrays.
var cr_series = [4, 2];
var ct_series = [2, 3];
function preload() {
// next I will iterate through all the attributes of the window object
// which includes my global variables, the arrays I have already
declared
for ( parm in window ) {

// if the current attribute contains the string '_series', use it
if ( parm.match('_series') ) {

//transform the current attribute by dropping '_series'
//which will result in for example 'cr_series' changing to 'cr'
//and call the loadArray function with the transformed attribute
loadArray( parm.replace('_series','') )
}
}
}

function loadArray( d ) {

//the variable x will 'reconstitute' my attribute name my adding
'_series'
var x = window[d+'_series'];

//my loop iterating variable should be the length of the user-defined
arrays
var j, i = x.length;

//A is an object
var A = [];

//do my loopy magic to create the multidimensional array
//with the apppropriate image names
while ( i-- ){
j = x[i];
A[i] = [];

while ( j-- ) {
A[i][j] = new Image();
A[i][j].src = 'images/' + d + (i+1) + '_' + (j+1) + '.jpg';
}
}

//aha! declare a new global array with a dynamically created name
window[d+'_images'] = A;
}
OK - I think I have it. And thanks for the Firefox tip, I will require
that my users set their browsers to allow javascript to change image
src. I think this is the default setting?

Thanks again,
-cjl

Jul 23 '05 #13
"cjl" <cj****@gmail.com> writes:
I've got an extremely ugly refactored function working, using eval()
You cansay that again.

If you want to dynamically create named references to something, there
is no need for them to be variables .Just make them properties of a
container object. Also, it's better to pass something directly than
to pass its name and need it looked up.
function preload()
{
if (cr_series.length >1) preloadIt('cr');
if (ct_series.length >1) preloadIt('ct');
if (cr_series.length >1) preloadIt('cr', cr_series);
if (ct_series.length >1) preloadIt('ct', ct_series);
}

var namespace = new Object();
function preloadIt(what)
{
function preloadIt(what, series) {
eval("" + what + "_images = new Array();");
namespace[what+"_images"] = new Array();
for (var loop = 0; loop <= eval("" + what + "_series.length-2");
loop++)
{
for (var loop = 0; loop <= .length-2;loop++) {
eval("" + what + "_images[loop] = new Array();");
namespace[what+"_images"][loop] = new Array();
for (var i =0; i < eval("" + what + "_series[loop]"); i++)
{
for (var i = 0; i < series[loop]; i++)
eval("" + what + "_images[0][i] = new Image();");
eval("" + what + "_images[0][i].src = \"images/" + what + "\" +
(loop+1) + \"_\" + (i+1) + \".jpg\";");
var img = new Image();
img.src = "images/" + what + (loop+1) + "_" + (i+1) + ".jpg";
namespace[what+"_images][0][i] = img;
}
}
}

whew. I guess it keeps me from repeating myself, but I bet if I look at
this code in the morning I won't remember what I did.
Good!

Anyway, you don't need to store the image object, they preload just as
fine without that, so this should be sufficient:
---
function preload() {
if (cr_series.length > 1) { preloadSeries("cr", cr_series); }
if (ct_series.length > 1) { preloadSeries("ct", ct_series); }
}
function preloadSeries(name, series) {
for (var loop = 0, n = series.length; loop < n, loop++) {
for (var i = 0; i < series[loop]; i++) {
var img = new Image();
img.src = filename(name, loop + 1, i + 1);
}
}
}
function filename(name, major, minor) {
return "images/" + name + major + "_" + minor + ".jpg";
}
---
Is there another way to accomplish this, without eval() ?


Always!

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #14
cjl
Lasse:

Thank you for your reply. I'm not sure that I understand it, but
that's probably because I am thick-skulled, and not a programmer. I
will keep reading, and try to figure it out.
Anyway, you don't need to store the image object...


The reason I was storing a globally scoped array of image objects was
that later in my script I need easy access to both the names of the
preloaded images (so that I can do things like next_image and
previous_image) and to the properties of the image objects (namely,
height and width). Some of my images are of different sizes, and I must
dynamically shrink them to the size of a container div before they are
displayed. I need access to the image objects to read in the heigth and
width before swapping the src and changing the height / width of the
displayed image.

Anyway, now I can see that there are better ways to achieve my goal, I
will happily abandon eval ()! Once I have a working solution I will
post a URL.

For those who are interested or learning like me, I found the following
article about eval() and performance:
http://builder.com.com/5100-6371-5169823.html

Thanks again,
cjl

Jul 23 '05 #15
cjl wrote:
Hey all:

Thank you all. I obviously have a lot to learn.

Rob, your last example was sweet. Let me see if I understand it:

//declare my 'user editable' arrays.
var cr_series = [4, 2];
var ct_series = [2, 3];
function preload() {
// next I will iterate through all the attributes of the window object
// which includes my global variables, the arrays I have already
declared
for ( parm in window ) {

// if the current attribute contains the string '_series', use it
if ( parm.match('_series') ) {
Yes, but it's not very efficient. Consider putting your prefixes into
an array (e.g. prefixArray = ['ct','cr',...] ) and iterating through
that instead (the window object likely has a lot of parameters to get
through and you only want a couple of them).
//transform the current attribute by dropping '_series'
It gets a substring of the attribute name by replacing the matched
portion with an empty string.
//which will result in for example 'cr_series' changing to 'cr'
//and call the loadArray function with the transformed attribute
loadArray( parm.replace('_series','') ) [...] function loadArray( d ) {

//the variable x will 'reconstitute' my attribute name my adding
'_series'
The name is 'reconstituted' on the right, x becomes a reference to the
array that is also referenced by '.._series'.
var x = window[d+'_series'];
[...]
//aha! declare a new global array with a dynamically created name
window[d+'_images'] = A;
}
Or you could do the '_images' bit in preload and just return A, so the
call from preload would be something like:

if ( parm.match('_series') ) {
var pre = parm.replace('_series','');
window[pre + '_images'] = loadArray( window[pre + '_series']);
...

and loadArray would end with:

return A;
}
OK - I think I have it. And thanks for the Firefox tip, I will require
that my users set their browsers to allow javascript to change image
src. I think this is the default setting?


By default Firefox allows images to be changed, only inveterate
fiddlers or the paranoid will turn it off (guess you know how I found
out...). :-)

--
Rob
Jul 23 '05 #16
cjl wrote:

<snip>
For those who are interested or learning like me, I found the following
article about eval() and performance:
http://builder.com.com/5100-6371-5169823.html


I had always wondered if that moron published that article after I
emailed him a reply to it showing how to do every one of those examples
without eval. Instead of trying to determine the value, and uses, of
eval, he was hunting examples of when and why to use eval and make it
sound like eval was a catch all. And it is - for the inexperienced
programmers. Even his division data is of my own making, as can be seen
in the thread he linked to, although he says (in the article) "I have
been able to determine..." when he did no such thing.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #17
On 10/07/2005 00:59, Randy Webb wrote:
cjl wrote:
For those who are interested or learning like me, I found the following
article about eval() and performance: [...]

When I read that, I assumed that the article would say the usual:
compared to the alternatives, eval is slow and clumsy, and should be
avoided in the majority of all code. Oh, how I wish that was the case. :(
I had always wondered if that moron published that article after I
emailed him a reply to it showing how to do every one of those examples
without eval.
Rubbish, isn't it? It even seems like he's trying to devalue use of the
elements collection by adding extra whitespace to make it seem like that
version is larger. Rather underhand considering code size appears to be
his only reason for preference.

[snip]
Even his division data is of my own making, as can be seen
in the thread he linked to, although he says (in the article) "I have
been able to determine..." when he did no such thing.


If you continue into that paragraph, he writes: "However, in some cases,
EVAL outperforms conventional coding." Your example was very specific
and simply doesn't occur in day-to-day code. Moreover, the eval function
(and is it really so difficult to say function, not command?) was only
faster in one particular browser, and slower in others. Finally, he
fails to mention that the numbers he's quoting must be for around 500000
iterations (based on your numbers), and that the performance impact of
either approach is negligible anyway (which you noted yourself in the
cited thread).

He says that there are already plenty of sites and books that recommend
bad uses for the eval function. Did he just feel like creating another?

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 23 '05 #18
cjl
Hey:

Since starting this thread a few days ago I have learned a lot, and
wanted to say thank you again to everyone who responded. I have since
abadoned the use of eval, and have now re-implemented everything using
the examples provided above (Thanks RobG). I still have some ugly code,
and one or two eval statements elsewhere, and no error checking, and
all sorts of mssing features, but I am on my way.

I also took RobG's advice, and started all my counting from 0!

Sadly, even though my horrendous eval based function was beastly, it
worked, and it worked in both IE and Firefox. I can't get my 'new'
version to work with IE...

If anyone has some time to kill, and wants to help me even further, my
rough draft is up at:

http://www.saintrays.net

My new and improved preload function does not seem to work in IE.

I'll repeat what I said in a different thread: before mocking my design
choices and coding abilities, keep in mind that I am a doctor
(radiologist) and not a programmer or designer. This web app will be
targeted to a very small group of people, and I will require that they
must view it in 'fullscreen' 1024 x 768 for it to render correctly,
hopefully in Firefox / Mozilla until I iron out the IE bugs. It will
be a sort of 'teaching file' of interesting cases when finished. The
fullscreen look is so that it can mimic the look of powerpoint, without
actually being as limiting as powerpoint. To see what I mean, click on
the "CR" button to load the plain film images, and double click on an
image. This zooms it. When zoomed, you can click and drag to pan.
Double clicking again zooms out. All of this is impossible with
powerpoint. Of course, a lot of functionality has yet to be
implemented.

Anyway, thanks again. Now to clean up the code, and add some actual
error checking to my functions.

-CJL

Jul 23 '05 #19
Michael Winter wrote:
On 10/07/2005 00:59, Randy Webb wrote:
cjl wrote:
For those who are interested or learning like me, I found the following
article about eval() and performance: [...]

When I read that, I assumed that the article would say the usual:
compared to the alternatives, eval is slow and clumsy, and should be
avoided in the majority of all code. Oh, how I wish that was the case. :(
Me too. I read the article before it was "published" and had a lot of
bad to say about it, and hoped it would never see the web but it did.
I had always wondered if that moron published that article after I
emailed him a reply to it showing how to do every one of those
examples without eval.

Rubbish, isn't it? It even seems like he's trying to devalue use of the
elements collection by adding extra whitespace to make it seem like that
version is larger. Rather underhand considering code size appears to be
his only reason for preference.


If he even understood it then it might make sense, but I truly believe
he was just a journalist out to sell a story.
[snip]
Even his division data is of my own making, as can be seen
in the thread he linked to, although he says (in the article) "I have
been able to determine..." when he did no such thing.

If you continue into that paragraph, he writes: "However, in some cases,
EVAL outperforms conventional coding." Your example was very specific
and simply doesn't occur in day-to-day code. Moreover, the eval function
(and is it really so difficult to say function, not command?) was only
faster in one particular browser, and slower in others. Finally, he
fails to mention that the numbers he's quoting must be for around 500000
iterations (based on your numbers), and that the performance impact of
either approach is negligible anyway (which you noted yourself in the
cited thread).


That is almost verbatim of what I tried to explain to him in emails
before it got published but he wasn't looking for any truth, he was
looking for "reasons to use eval" of which I only know one:

To execute unknown (at runtime) code.
He says that there are already plenty of sites and books that recommend
bad uses for the eval function. Did he just feel like creating another?


Whether he felt like it or not, he succeeded in doing it.

It might turn out to be a good thing though. If people reading it end up
here, in clj, then they can learn the proper ways and finally realize
how ludicrous that article is.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #20
cjl wrote:
<snip>
I'll repeat what I said in a different thread: before mocking my design
choices and coding abilities, keep in mind that I am a doctor
(radiologist) and not a programmer or designer.


I hope you didn't take my comments as mocking you in any way, it wasn't.
The article that you referred to uses some data and code that I wrote a
while back and he twisted it around to imply that eval was the way to go
when it isn't. I was ranting totally about him and not you.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #21
cjl
Randy:

I think you misunderstood my post. I was trying to poke some fun at
myself and my programmig abilities (or lack thereof).

I know you were discussing the article, and I'm starting to understand
why you and others think it's a piece of junk.

Plus, if you were making fun of my code, you would be right. It's a
mess, and I stink! But I'm having fun learning, and I'm slowly getting
better at it.

Thanks for all the helpful comments, and please, keep them coming.

-CJL

Jul 23 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Frans Englich | last post by:
As continuation to a previous thread, "PyChecker messages", I have a question regarding code refactoring which the following snippet leads to: > > runner.py:200: Function (detectMimeType) has too...
4
by: | last post by:
Hi, Refactoring a winform causes problems if moving this form to a subdirectory in the same project. What is the workaround for this and will this be fixed in future? Thanks /BOB
2
by: Sachin Garg | last post by:
Hi, I was trying to find (like many others here) a tool for refactoring C++ code as I have lately been noticing that I spend most of my coding time doing refactoring and some refactoring which...
4
by: Champika Nirosh | last post by:
Hi All, We have developed stand alone application and it was started small and now has become big and we are on our way to structure it. (Thought a backward process we got to do it) So right...
15
by: Simon Cooke | last post by:
Does anyone know of any tools for refactoring header files? We're using a third party codebase at work, and pretty much every file includes a 50Mb precompiled header file. I'm looking for a tool...
0
by: bryan rasmussen | last post by:
Hi, I'm doing a sort of symbolic linking app in Windows for my own enjoyment, and figured I would do it in python for the same reason + learning the language. The following functions could...
83
by: deppy_3 | last post by:
Hi.I am started learning Programm language C before some time.I am trying to make a programm about a very simple "sell shop".This programm hasn't got any compile problem but when i run it i face...
7
by: Kamilche | last post by:
''' I'm in the middle of a refactoring dilemma. I have several singletons that I'm turning into modules, for ease of access. The usual method is noted as 'Module 1' below. The new method is...
20
by: Mike | last post by:
Hi all, I am cramming for an technical interview about C++ and programming... I believe there will be problems on data-structures and algorithms... could anybody recommend some online...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.