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

Should I always use instanceof and typeof for type checking, e.g.

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 an ifof instance and trust this argument? Interesting, because this is the only weakly typed language that I used, so I'm a little uncomfortable, not always knowing the type of object you are working on.

+6
source share
4 answers

There is no “right” answer for all situations. Regardless of whether your code should check the type of incoming arguments, it depends on many factors:

  • What is the probability of a wrong argument? If you are the only client of this function, you can decide whether your function should be protected from its own programming errors. If this is just a regular web page, and not a particularly important operation, of which this function is a part, then there is no practical reason to add additional code, since you control the correct use of this function and transfer the corresponding data. If, on the other hand, you are writing an API that will be used by a lot of people, and it is really important that your code either succeeds or follow the documented error path, then you may well want to check your arguments so that you can act in 100% predictable way if something is wrong.

  • Can you do anything particularly useful if you find this to be the wrong type? . If you just don’t give in a few lines of code earlier and fail just like you would if you checked the type first, then there can be no advantage to checking it earlier.

  • What are the consequences of an error in the type of this argument? If the consequences (taking into account the scope of this function) are not so important, then again, perhaps, additional code is not worth it. If, on the other hand, you expect to complete an important transaction, and you absolutely want to protect this operation from the maximum possible amount, which could be erroneous, then, in any case, check each argument for type and range.

  • Where is the data coming from? If the data comes from the end user or from some external source that you do not control, then you will almost always want to check it before use.

  • Will this code be used in many situations by many different developers? If this is the case, then you will probably want to fail early with proper error / debugging messages to inform consumers of your function as early as possible in their development that they are not transmitting the correct data.

  • Do you specifically allow the transfer of data of different types at specified points in the argument list? One common way to overload functions in javascript is to allow the same function to accept several different types of arguments, perhaps doing slightly different things based on what is being passed. For example, jQuery does this all the time. If you do this, you will have to check the types of arguments to distinguish between the different ways this function can be called. And in doing so, you may also find unsupported argument types.

  • How low / high level function is this? If this is a rather low-level function, far from the original data source, then you probably want to perform type checking at a higher level closer to the original data source, rather than checking the type at each intermediate level of the function call.

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

  • Should the concept of this function be strict or permissive? . By the nature of their context, some functions are more useful if they are working hard to get a useful value from any input given. Other functions are useful only when they do only what they assume, without even trying to force the questionable data to be involved in something useful. If you are calculating the dosage of insulin, you really do not want to ever have any chance that the input was not exactly the way you interpreted it. But, if you are calculating a temporary estimate for an operation that is only to be used, you need to temporarily display it to the end user, you are likely to just do your best with any data provided.


If you need a really quick answer for your average type of web page, I will usually check all the data coming from external sources or from end users, and I trust the data coming from other parts of my own code. Obviously, there are many gray areas between or some different types of code and that I allow these other factors to influence the decision.

+6
source

If you need something of a certain type, otherwise it breaks, then check its type or force it to the appropriate type. If value is also a problem, you should also check its value.

In fact, it all depends on the source of your arguments. If this is just your code, then there should be no reason to type check if your code is correctly written. If it is an external source (i.e. you are writing a library function or one that accepts user input), you should check this argument.

Simply put, check types when you have a reason to expect them to not be of the appropriate type. Otherwise, just write your code so that there are no type mismatches.

Rejecting dynamic typing will force you to ultimately struggle with the language, rather than using it.

+6
source

Just believe it by default. Check types only if you need to work fast.

In languages ​​such as JS (and Ruby and Python and similar dynamically typed languages), duck typing is really common. By explicitly checking the type, your functions become less general and therefore less useful.

As an example, many standard JS functions in Array.prototype , while designed to work with Array instances, are intentionally encoded to allow any object that supports the same operations as the array to be passed, even if not on actually a Array . (Find the phrase “intentionally generic” in the ECMA-262 standard .)

+4
source

Document your code correctly, and other developers who will use your code will know what type your function supports. If you do not have a reliable reserve, then there is no need to check the type of the object, because JavaScript can independently execute the error. And, obviously, if there is no data loss in danger, in the end it is safe to exit the program.

+2
source

Source: https://habr.com/ru/post/972547/


All Articles