Facebook's Flow could help JavaScript programmers spot elusive bugs

PCWorld News

Easing what has traditionally been a difficult programming task, Facebook has released as open source a debugging tool for JavaScript, called Flow, designed to highlight problems caused by the misuse of data types.

“Flow improves speed and efficiency so developers can be more productive while using JavaScript,” Facebook engineers said in a blog item posted Tuesday. Facebook has used Flow for many of its own projects.

Flow is a static type checker, one that ensures that when a program is run that its variables, functions and other elements of code will adhere to their original specifications. It can check to see if a value assigned to a variable is the correct type, for instance, a number rather than a string of characters. It can also check to see if a function is passed the correct number of inputs.

Such error checks could prevent program failures, and help secure the program against malicious misuse. Type checking can also aid in maintaining the program’s code base.

JavaScript has grown increasingly popular in the past few years as a Web development tool, not only for adding functionality to Web pages, but also for creating full-scale Web applications. Unlike more traditional programming languages such as C++, JavaScript does not provide any native static typing capabilities. Because it is a very expressive language, JavaScript does not lend itself easily to type-checking systems, the Facebook engineers wrote.

Other companies have tackled the challenge of adding static type checking to JavaScript. Microsoft, for instance, developed TypeScript, a superset of JavaScript code that can be compiled so type checks can be conducted.

Unlike TypeScript, Flow allows the developer to check only portions of the code. JavaScript frameworks often use techniques, such as reflection, that make it difficult to use type checking.

Running Flow does not require the programmer to compile the program, or otherwise prepare the program for inspection. Instead, it works as a background process. Flow can check standard primitive types such as numbers, strings, and Boolean values as well as structured types such as functions, objects, and arrays.

Flow is initially available as a plug-in for both the Emacs and Vim code editors, and versions for other editors may be released in the future. It also offers a API (application programming interface) so it can be used by other code analysis tools.

Facebook will routinely release tools as open source that company engineers originally developed for internal use, in hopes that others will further develop the tools. The company has released a virtual machine for running programs written in PHP, called the Hip Hop Virtual Machine. It has also released a popular JavaScript library for building user interfaces, called React, and a database engine for querying distributed sets of data, called Presto.

To comment on this article and other PCWorld content, visit our Facebook page or our Twitter feed.
Shop Tech Products at Amazon
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.