• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Is there a recommended way to catch undefined parameters in a function?

Participant ,
Mar 22, 2012 Mar 22, 2012

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()&&parameter2.ObjectValid())

      { // execute code here

       } else {Err(foobar.name+' has an unitialized parameter')}

}

TOPICS
Scripting

Views

683

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 22, 2012 Mar 22, 2012

Copy link to clipboard

Copied

Or i could do this, I suppose:

function foobar (parameter1, parameter2)

{

     if (parameter1!=undefined&&parameter2!=undefined)

     { // execute code here

       }

  }  else {Err(foobar.name+' has an unitialized parameter.')}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 23, 2012 Mar 23, 2012

Copy link to clipboard

Copied

LATEST

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 ')}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines