It depends on what you want. Want to have an organized code or just "work"? You can let the error happen at its end point. Or you can show earlier where the error actually occurred.
Polluted code
I would not call pollution something that is useful. Pollution is unnecessary thing. Whether you choose to facilitate code maintenance, usage by others or by yourself in the future. If you want to give better information to debug, information in the right place is critical.
In fact this code is actually quite simple, if it’s not, it’s doing something wrong. It’s very fast to encode this. This tiny amount of time will be compensating the first time you debug.
Exception
I don’t know if the solution is to use one throw
. But some treatment may be interesting. The use of exceptions is not part of the Javascript culture. It is usually used in more obvious situations where there is a gain doing so. I don’t know if this is changing or will change with increasingly complex applications but the fact is that this feature is not used as it is used in other languages.
And maybe it’s a good thing since most programmers use it the wrong way. In this case I do not know if it is for an exception since we are talking about a programming error. The ideal is to do something that helps the programmer find and understand the error more easily. Exception is not the best mechanism for this. I even understand this confusion since the exception in other languages is used to solve any kind of problem, so programmers don’t really understand the various types of problems.
On the other hand if you don’t know what to do, if you treat the problem wrong, don’t add relevant information, you will add useless code.
Performance of impaired application
First of all this will make very little difference. We’re talking about Javascript, a language that wasn’t meant to perform as well as possible.
And second, back to the previous item, what is best for your code? What will make your life easier? Even in other languages performance should only be a concern when you have measured and seen that there are problems, that something is running slower than is acceptable.
This is not a reason to avoid such verification.
Alternative view
But that depends a lot on how you and your team work. If you have a proper workflow and tools, this may not be so necessary. Most of the time what you will lose is a better location of the problem. You may have to check the whole call stack to understand where the problem originated.
Some people prefer to do this in functions considered "library" and not in the application functions.
The smaller the team and the application you are running, the less the need for these checks (which I wouldn’t call validation since this is a programming problem and not data entry).
Alternative forms
Unit tests
Other people will say that it is critical to do unit tests that do these checks for you. That is, the verification is done but outside the code.
Alternative language
You can use some other language that compiles for Javascript and has type checking at compile time. An example is Typescript.
Actually a language like Typescript will bring many other advantages, especially if you use the tools available to encode in it. Unlike other languages that try to run on top of JS it behaves almost autonomously. I never liked layers on top of the JS but this case seems to me to be an exception. The advantages brought and the near absence of disadvantages make us think about their use.
Would something like this:
function facaAlgo(str : string) {
// ...
}
// em algum outro lugar:
var i = 2; // tipo de i inferido como numérico
facaAlgo(i); // erro de compilação!
Completion
Anyway, it is more a matter of taste and adaptation to the working method. Test each way for a while and try to find the best alternative for you. I always prefer language to do this for me. Then, I look for tools external to the code to help and only if none of this is good and available I will see if I will put this type of verification in the code when the application is a little more complex (which is not usually the case for JS applications websites).
Recommend my other answer on the subject in that reply.
I think you would like to meet http://www.typescriptlang.org/
– Paulo Roberto Rosa
@Pauloroberto I have heard but never used. Have you used? It is difficult to integrate with existing code?
– user18612
Really??? Write code to validate types in a dynamic language??? I die and don’t see everything. Your question makes it seem that you have received or seen this kind of recommendation. Can you pass the source? I would like to understand the reasoning of this.
– Caffé
@Caffé I did not receive this recommendation from anyone, but me I constantly wonder whether it is good practice to make these validations or not. I would like to hear more about your opinion about validations not being required.
– user18612