3 ms·
TIL relying on a bunch of boilerplate “typeof o.prop” checks is “clean, elegant, expressive code”, while knowing implicitly that o.prop is always an integer is
by stephenr 6y ago
TIL relying on a bunch of boilerplate “typeof o.prop” checks is “clean, elegant, expressive code”, while knowing implicitly that o.prop is always an integer is not?
- dragonwriter 6y ago> TIL relying on a bunch of boilerplate “typeof o.prop” checks is “clean, elegant, expressive code”, while knowing implicitly that o.prop is always an integer is not? Clean and elegant are certainly debatable, but, yes, code that is explicit in its preconditions (whether through explicit runtime checks or explicit static declarations or other mechanism) is pretty clearly more expressive than code that relies on the developer “knowing implicitly” the same information.
- stephenr 6y agoWhen I said knowing implicitly I was referring to for example, declaring that class `Foo` has an integer property `prop`, and function `bar` only accepts an instance of `Foo` as argument `obj`, thus `obj.prop` is always an integer. The developer doesn’t need to add typeof checks on the argument, on the property, etc. The above comments point to (a) not using classes or declared properties at all, and regardless JavaScript has no support for automatic type checks on properties or function arguments.