Copy link to clipboard
Copied
I was working on a script that had a function which recursively called itself. At one point the function had one parameter but eventually I added a second parameter. Unfortunately, i forgot to initialize the second parameter when i recursively called the function within the function. In the extendscript toolkit, this meant that when I hit the part of the function that required the parameter, it would throw an error "object is undefined" without telling me what the object was so I didn't know the issue was the uninitalized parameter. I wish the toolkit was robust enough to instead throw an error like "parameter [name of parameter] is undefined in function [name of function]". That would have immediately told me what was going on.
So is there some way to define a custom function or some test i could add in that would catch the unitialized parameter problem? The one thought i had was to always put an ObjectValid test on the parameters at the beginning of the function like so:
function foobar (parameter1, parameter2)
{ var vFunctionName=foobar;
if (parameter1.ObjectValid()&¶meter2.ObjectValid())
{ // execute code here
} else {Err(foobar.name+' has an unitialized parameter')}
}
Copy link to clipboard
Copied
Or i could do this, I suppose:
function foobar (parameter1, parameter2)
{
if (parameter1!=undefined&¶meter2!=undefined)
{ // execute code here
}
} else {Err(foobar.name+' has an unitialized parameter.')}
}
Copy link to clipboard
Copied
Or a different way would to be test that the number of arguments passed in is equivalent to the number of arguments declared in the function. Here's the test:
function createBookList(pFolder,pBookArray)
{
var vFunctionArgs=createBookList.length;
var args = [].slice.call(arguments);
if (vFunctionArgs==args.length){$.writeln ('the number of parameters passed in MATCHES the parameters declared in the function')}
else {$.writeln('the number of parameters passed in do NOT match the parameters declared in the function ')}
}