3 ms·
Agreed, however we look at it there is no way to change the several dozen JSON libraries out there. People should avoid using undefined, not avoid using null.
by dheera 6y ago
Agreed, however we look at it there is no way to change the several dozen JSON libraries out there.
People should avoid using undefined, not avoid using null.
- ZephyrBlu 6y agoAvoiding undefined is more preferable to me, since then it only shows when when you screw up.
- anuila 6y agoHow can you avoid using undefined if it's the default value for `prop.nonexistent` and `return`? Have you ever used JavaScript?
- afiori 6y agoone 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.