Replacing guesswork with assurance

One of the nice features of a dynamically-typed language like JavaScript is that you can write code without having to think about types. Types are good and they do solve a lot of problems—the point I'm trying to make, believe it or not—but sometimes you need to be able to just write code without having to formally validate for correctness. In other words, sometimes guesswork is exactly what you need.

If I'm writing a function that I know takes an object as an argument, I can just assume that any object that's passed to my function will have the expected properties. This allows me to implement what I need to, without having to make sure that the correct types are passed as arguments. This will only work for so long, though. Because invariably, your code will get something unexpected passed to it as input. Once you have a complex application with many moving parts, type-safety can remove the guesswork.

Flow takes an interesting approach. Instead of compiling new JavaScript code based on types, it simply checks that the source is correct based on type annotations. These annotations are then removed from the source so that it can run. By using a type checker like Flow, you can be explicit about what each of your components is willing to accept as input, and how it iterates with the rest of the application by using type annotations.

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

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