3 ms·
one can argue that if you obtain a value in such a way you should validate it first or not use it at all
by afiori 6y ago
one can argue that if you obtain a value in such a way you should validate it first or not use it at all
- rbanffy 6y agoFurthermore, if the function returns `undefined`, as in "it's not expected to return a value", you should probably not try to assign its return value to anything you'll see again.
- dheera 6y agoIn general I would validate for the thing I'm actually looking for instead of validating against undefined and other invalid input values. For example: function setContrast(value) { if(typeof(value) !== "number") throw "input must be a number"; if(!(value >= 0 && value <= 1)) throw "input must be between 0 and 1 inclusive"; // ... rest of code ... } This pattern will generally avoid any issues with undefined values being passed in.