Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JavaScript, how much trust should I have that function arguments are of the correct type? [closed]

Tags:

javascript

Should I always be using instanceof and typeof to check types eg

addRow : function(rowBefore) {
    if(rowBefore instanceof Y.PopulateList.makeRow) {
        this.allRows[row.toString()] = row;
        row.altered = true;
        Y.DragAndDrop.addNewDrag(row.rowDiv);
        node.insert(row.rowDiv, 'after');

    }
    else {
        console.log('not adding a makeRow');
    }  
},

or is it acceptable to leave out the instanceof and trust that the argument is valid? I wonder because this is the only weakly typed language I have used, so I am slightly uncomfortable with not always knowing the type of the object being acted upon.

like image 874
michaelAdam Avatar asked Jul 21 '14 02:07

michaelAdam


People also ask

What are function arguments in JavaScript?

Function arguments are the real values passed to (and received by) the function.

What is the type of argument inside a function in JavaScript?

The arguments object is a local variable available within all non-arrow functions. You can refer to a function's arguments inside that function by using its arguments object. It has entries for each argument the function was called with, with the first entry's index at 0 .

What is .apply in JavaScript?

apply() The apply() method calls the specified function with a given this value, and arguments provided as an array (or an array-like object).


1 Answers

There is no "right" answer for all situations. Whether or not your code should check the type of incoming arguments depends upon many things:

  1. What is the likelihood of a wrong argument? If you are the only customer of that function, then you can decide whether your function needs to protect itself from your own programming mistakes. If it's just a regular web page and not a particularly important operation that the function is part of, then there may be no practical reason to add the extra code as you are in control of making sure this function is used appropriately and passed appropriate data. If, on the other hand, you're writing an API that lots of people will use and it's really important that your code either succeeds or follows a documented error path, then you very well may want to check your arguments so you can act in a 100% predictable path if something wrong is passed..

  2. Can you do anything particularly useful if you detect it is the wrong type? If you're just going to fail a few lines of code sooner and fail in no different way than you would if you had checked the type first, then there may not be any advantage to checking it sooner.

  3. What are the consequences of a mistake in the type of that argument? If the consequences (given the scope of what this function is trying to do) are just not that important, then again it may not be worth the extra code. If, on the other hand, you're expecting to complete an important transaction and you absolutely want to protect that operation against as many things as possible that could accidentally be wrong, then by all means, check every argument for type and range.

  4. Where is the data coming from? If data is coming from the end-user or from some outside source that you don't control, then you will pretty much always want to check it before consuming it.

  5. Is this code going to be used in lots of situations by lots of different developers? If so, then you probably want to fail early and with proper error notifications/debug messages to tell the consumers of your function as early as possible in their development that they aren't passing the right data.

  6. Do you specifically allow data of different types to be passed in given spots in the argument list? One common way of overloading functions in javascript is to allow the same function to take several different types of arguments to perhaps do slightly different things based on what is passed. For example, jQuery does that all the time. If you are doing that, then you will have to check argument types just to distinguish between the different ways that this function is allowed to be called. And, in doing so, you may also discover unsupported argument types too.

  7. How low/high a level function is this? If this is a pretty low-level function, far from the original data source, then you will probably want to do your type checking at a higher level closer to the original source of the data rather than do type checking at every intervening level of function call.

  8. How important is performance? If the performance of this function is very important because it's called very often and often called from within loop structures, then you may want to make sure that any necessary type checking is done at a higher level and not done in one of your bottleneck main functions.

  9. Should the concept of this function be strict or permissive? By nature of their context, some functions are more useful if they use their best effort to produce a useful value from whatever input they are given. Other functions are only useful if they only do exactly what they are supposed to without ever trying to permit questionable data to be coerced into something useful. If you're calculating an insulin dosage, you really don't want to ever take any chances that an input wasn't exactly meant to be what you interpreted it as. But, if you're calculating a time estimate for an operation who's only use is to be temporarily displayed to the end-user, you probably just do your best with whatever data is provided.


If you wanted the really quick answer for your average type of web page, I will generally check all data coming from outside sources or from end-users and I will trust data coming from other parts of my own code. Obviously, there are many gray areas in between or some different types of code and that's where I let these other factors influence the decision.

like image 95
jfriend00 Avatar answered Sep 19 '22 10:09

jfriend00