Removing runtime checks

The solution to handling data with unknown types in dynamic languages such as JavaScript is to check values at runtime. Depending on the type of value, you might have to perform some alternate action to get the value that your code is expecting. For example, a common idiom in JavaScript is to make sure that a value is neither undefined or null. If it is, then we either throw an error or provide a default value.

When you perform runtime checks, it changes the way you think about your code. Once you start performing these checks, they inevitably evolve into more elaborate checks and more of them. This frame of mind really amounts to not trusting yourself or others to call code with the correct data. You think since it's likely that your function will be called with junk arguments, you need to be ready to handle anything that is thrown at your function.

On the other hand, embracing type-safety means that you don't have to rely on implementing custom solutions to defend against bad data. Let the type system handle this for you instead. You just need to think about what types your code needs to work with, and go from there. Think what does my code need, not how do I get what my code needs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.175.182