Facebook's Hack language a silver bullet for user-developers

Write Something: The original Facebook wall. Credit: Simon Bisson

How do you modernize the way we build the web? That's the question at the heart of the release of a flurry of new languages and development platforms -- all from companies that run and build large scale web services. First there was Google's Dart, then Microsoft's TypeScript, and even Twitter has its Bootstrap front-end framework. Now Facebook has joined the pack, with the release of Hack, a new language for its HHVM runtime.

While getting more out of CSS is good, as is replacing JavaScript; Facebook's HHVM and Hack combo is targeting one of the foundations of the modern web: rapid application development. It's never been particularly easy to test and debug web apps, and you'll find yourself in an apparently endless loop of editing code, uploading, testing, debugging, re-uploading code and restart servers. It's a tedious process, and one that Facebook feels can be streamlined with Hack.

Much of Facebook's work on improving the venerable PHP web development platform has been around improving performance. While the backend technologies that power Facebook have changed over the years, much of its front-end is still built using PHP. That's given the massive social network a unique perspective on PHP's scalability. The result was HVVM, a just-in-time compiler for PHP code that makes in-line code in web pages run considerably faster, without having to compile pages to C++.

Now it's introduced the next step in its refactoring of PHP: the Hack language. Despite a slightly unfortunate name that makes it hard to find code using Google (just try searching for "hack sample code"; you won't find a reference to the language until the second page of results!), Hack is a  well thought-out modernization of a popular development platform that's been designed in conjunction with the HHVM virtual machine.

Credit: Simon Bisson/ Facebook
Facebook's Hack site includes an online interactive tutorial to get you started.

While Hack owes a lot to PHP, it's better considered alongside more modern languages like C#, as it brings constructs like generics and collections to web development. However, most importantly, it brings typing to a PHP-like language. While most PHP developers know when they're using a string or an integer, adding types to variables makes your code more readable -- and easier to debug. If you know $x is a string, you won't be trying to use it in a calculation.

HHVM includes a type checker that handles a mix of statically typed code and legacy PHP dynamic typing. As you convert code from PHP to Hack you can add typing to variable declarations and to methods. The type checker doesn't just work at run time, it monitors the file system, so you can get warnings of errors before you start running your application.

More complex features include support for generics, letting you write code that can be used with different types depending on how it is called, and for collections, which improve on PHP's arrays for data storage. If you're creating web services that are intended to work with mobile devices there's also the option of adding asynchronous code to your Hack applications that will suspend until a response has been received. Facebook has done a lot of work in mixing new features with the familiar PHP way of working, so you don't need to dive into Hack right from the off.

Getting started with Hack is easy enough. All you need is HHVM, and you'll find that most PHP code will run without any changes (Facebook has removed support for some deprecated PHP features in Hack, as they conflict with some of the new Hack constructs, especially static typing). You can then start converting it to Hack, with both Hack and PHP running in the same page. Taking a slow approach to Hack development makes a lot of sense, as you can start with existing PHP pages and code and slowly add Hack features as you need them -- and once they're in place you take advantage of them in new pages and in new applications.

New languages and tools that make it easier to build apps are always a good thing. The web we build on is 25 years old, and it's been through many cycles of development technologies: from Perl scripts and CGI, to ASP and PHP, to JavaScript and AJAX. We've stretched the modern web to its limits, and it's now time for a new generation of tools that aim to make it easier to build and debug large and complex web apps.

That's why we're seeing so much interest in new tooling from the cloud-scale vendors. They've struggled with the last and current generation of tooling, have built and tried tools that take desktop programming and translate it to the web, and are now looking at web-centric ways of building apps. Hack is Facebook's answer to that question, and delivering it as a near drop-in replacement for PHP makes a lot of sense.

With Hack now an open source project, it's going to be interesting to see just how well the PHP community at large picks up the language. We'd hope to see support for it on other PHP virtual machines and parsers, making migration easier -- and with competing engines, the possibility of competition driving performance improvements. Support for Hack on multiple runtimes will also make it easier for BYODev developers, as it will mean code can be shared easily, and migrated between cloud services and providers.

Should you consider using Hack for a BYODev-style project? Today's apps aren't built on perfectly crafted code; and when we're building simple single purpose apps that aren't intended to stay around more than a few weeks, it's well worth considering. If you're able to install HHVM on your web servers we think it's certainly worth a look, if just for the ability to edit pages live on a server without having to restart an application every time you make changes.

Facebook's informal motto is "Move fast and break things". With faster debugging, a language like Hack can help you do just that in your own code; which will help you learn, and help you deliver the app you want to build. That's a big win for anyone building their first services and apps.  

Join the discussion
Be the first to comment on this article. Our Commenting Policies