JRS: In article <7b2c028b.0504140645.66e34503@posting.google.com >,
dated Thu, 14 Apr 2005 07:45:07, seen in news:comp.lang.javascript, Mike
<mike.renwick@uk.standardchartered.com> posted :[color=blue]
>After reading much on the evils of eval, I have a question using my
>own personal use of the function...
>
>We have a reports system that will generate reports based on a number
>of parameters available on a blotter at the top of our report system.
>Each report could (and does) use a combination of some (but not all)
>of these 15 parameters.[/color]
But each report function can be given, with negligible overhead, all 15
parameters - you're only passing the identity of an object, and within
the report function you can give sensible names to the wanted ones and
names such as x0 x1 ... to the others. Calling them all x would be
inelegant, unless it can be shown both legitimate and safe.
You can now put the report function names in an array
function RepFn0(p1, ..., p15) { ... }
function RepFn1(p1, ..., p15) { ... }
function RepFn2(p1, ..., p15) { ... }
....
var AllRepFns = [RepFn0, ...]
[color=blue]
>Each report also has a series of precalculations that need to be run
>before the report can execute.[/color]
The entries in AllRepFns can be enhanced to be objects containing not
only the function name but also a list of names of precalculations.
....
[color=blue]
>My solution was to create a string for each report
>"ReportName~parameter~parameter~parameter"
>built dynamically, and eventually parse the string in an execution
>function that resolves it to a full function call
>
>"ReportName('parameter','parameter','parameter'); "
>which is the Evalled to execute[/color]
.... and execute by AllRepFns[n](P1, ..., P15)
Or the array could be generalised into a simple Object, using the name
of the report to select the correct RepFn# or the Object holding it.
You end up with table-driven control, with the table held in objects
and/or arrays.
Take a glance at "Data", in the textarea, at the end of <URL:
http://www.
merlyn.demon.co.uk/holidays.htm>, and similar in the source.
[color=blue]
>As this is only a single line of code to Eval, it seemed the most
>logical and probably reasonably inexpensive.[/color]
All you need do is see if the process is significantly slower using eval
for (say) the quickest and slowest functions in comparison with a direct
call, on your slowest target machine. Not detectably but significantly.
[color=blue]
>My question is is this a common misuse of eval, or one of the few
>cases where it makes sense to use? I realize that my example doesn't
>necessarily flesh out enough detail regarding judgement of the entire
>report launching concept, but trust me when I say that "eval" has cut
>my code down by several 100 lines![/color]
It is at worst a rare misuse, IMHO.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.