Dealing with optional arguments

Arguments to procedures in VBA can be optional, and optionally they can have a default value. For example

Public Function create(par As cDataRow, colNum As Long, rCell As Range, _
            Optional keepFresh As Boolean = False, _
            Optional v As Variant) As cCell 

We can also test for the presence of a variant argument by using isMissing(). See Getting Started with argument passing for how all this works in VBA
    If IsMissing(v) Then
        pValue = rCell.Value
    Else
        pValue = v
    End If

In javaScript though, all arguments are optional. Consider this
function a ( arg1, arg2) {  ... }

There would be no complaints if you called this function with 0, 1 or 2 arguments, but how to detect what was passed?

Let's assume that arg1 was an object.
if(!arg1) { ... }

would be good enough to test if a had been passed with a valid object address, but if arg1 was, say a boolean, and had the value false, then !a would yield the same result as if arg1 was not present. So we need another way. 

Since javaScript will convert to a string to compare to another string, comparing a missing value against a string will cause it to be converted to 'undefined'. We can leverage this as below to detect a missing argument.

if (arg1 == 'undefined' ) {... arg was missing ...}

However, this would fall apart if 'undefined' was a valid value for arg1. 

typeof Operator to test for optional/missing argument

Luckily we can use the typeof operator instead.

if (typeof arg1 == 'undefined') {... arg was missing ...}

Since we are going to be using this a lot, let's make a convenient function of it
function isUndefined ( arg) {
  return typeof arg == 'undefined';
}

So in future we can conveniently say, 
if (isUndefined(arg1)) {...arg1 was missing .. }

And to minimize conversion, do a vbaHack of
function IsMissing(arg1) {
    return isUndefined (arg1);
}

Dealing with default value for optional argument

We are going to convert an existing VBA project, though, so it's still going to be a pain to deal with each optional argument, especially if it has an optional parameter. Here's a generalized function we can use to deal with missing arguments and their default value
function fixOptional (arg, defaultValue) {
  if (isUndefined(arg) ){
    if (isUndefined(defaultValue) ) 
      MsgBox('programming error: no default value for missing argument');
    
    return defaultValue;
  }
  else 
    return arg;

and here is how we use it it.
 function x ( par , colNum, rCell, keepFresh, v){
  // deal with optional parameters
  this.value = fixOptional ( v , rCell ); 
};

Transitioning is covered more comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly.



For more like this, see  From VBA to Google Apps Script . Why not join our forum,follow the blog or follow me on twitter to ensure you get updates when they are available. Much of  the code referred to here is the mcpher library. You can find out how to include it in your project here.

Comments