Facebook’s Hack Programming Language

posted by on 11th April 2014, at 8:15pm | Discuss Article

Hack is a new programming language that has been pioneered by Facebook. Facebook traditionally wrote their code in a language called PHP (the same language we use). PHP has been around for years and is one of the definite go-to standards for server side web development. PHP has grown over the years to offer more modern features to developers, as an example, PHP 5 brought in proper object oriented programming. Though PHP has continued to grow and become more modern there are still weak points within the language itself compared to more modern languages or its desktop counterparts. This combined with the high performance work environment of Facebook lead to the creation of Hack.

Hack is a derivative of PHP, in its simplest way it is what a modern version of PHP should be. Standard PHP code written today is compatible with Hack. This is just the very beginning, even though it may appear that on the surface there are very few differences between PHP and Hack once one delves deeper into the Hack language the true differences appear. Hack sets out to solve some of the issues with standard PHP and bring the language into the future. PHP is a great language to work with because of its speed and it looks a lot like C-based languages which most programmers are familiar with. C and C++ have already been modernized on the desktop with C# on Microsoft platforms and Objective-C on Apple platforms. Hack is the next logical step for modernizing the C of the web, that of course is PHP!

It’s not every day that a new programming language comes around. When they do they often go through the same early growth phases that social networks do. That is, in the beginning there are only a couple of early adopters but often those that do are extremely passionate. There also needs to be a reason for the new language to exist. In recent years most new languages have come because of the demands of programmers to make code easier to write. PHP is an easy language to write with in the sense that within a couple hours a person can have a working content management system with basic features. Where PHP falls back is that it doesn’t provide many of the niceties that developers have become accustomed to on the web with languages such as node.js or Rails. The Hack website at hacklang.org goes into great detail as to what benefits Hack provides. Upon seeing this website I knew this is something that I would be interested in looking into since I’ve started to find vanilla PHP increasingly painful to work with.

Hack makes me excited because it provides a nicer PHP type language for web use. It also opens up the doors to many aspiring developers who already know a C type language or PHP itself. Hack also provides a great number of features that I’m going to highlight that are already presenting themselves as extremely useful. Here’s a list of some of the features that I am particularly excited about:

  • Type annotations
  • Asynchronous methods
  • Better collections (maps, vectors, sets, etc.)
  • Generics
  • Type aliasing
  • XHP
  • Strict mode

First and foremost are type annotations. Type annotations brings typing (variable types such as int, float, string, etc.) to PHP for parameters, member variables, and return types. Anyone who has programmed with Java or a multitude of other languages will know that typing is important. PHP has long been a language where the types of variables were inferred based on the data they held or what functions operated on that data. Not the case with Hack. Hack can explicitly say that such a variable must be an int or a method must return a string. This means that methods can be tailored specifically to a certain data type and their execution will be more predictable. Also errors can be caught before execution, this means that bugs will not suddenly appear when a wrong data type is input into a method.

Hack provides asynchronous methods. Parsing out the term asynchronous methods simply means that more than one method can be executed at the same time without causing performance issues in your main application. Desktop programming languages have had this concept for years with threads which allow for the application to perform background tasks while allowing the user interface to be fully controllable. This became highly used on the web with AJAX (asynchronous javascript and XML). All a programmer has to do to make a method asynchronous is preface it with the “async” keyword.

The support for more diverse collections in Hack is appealing since arrays are generic in PHP. Hack provides vectors, maps, sets, and pairs. Each one of these are slightly different than a standard PHP array. A Vector is an ordered, index-based list collection. At its core Vectors are as close as one gets to a standard array, simply indexed from 0 to n-1 items in a list. Maps are dictionary based collections which allow for custom keys and quick lookups. This means that a programmer can create a key called ‘identifier’ and then write the code $item[‘identifier’] = 1234 and then access the value by simply calling $item[‘identifier’]. Sets are list based but only store unique values. Finally, a pair is Vector that only supports storage for two values. Collections modify PHP in the same way that type annotations modified standard PHP variables, that is create a stronger sense of type among the many uses of arrays in PHP.

Generics are a somewhat of an abstract concept for newer programmers. The simplest way of thinking of a Hack generic is a template for classes or methods. This means that as a programmer you will create the “generic” template of what your class or method should ultimately end up doing. A great example of this is the Hack Vector class. The Vector is designed as a generic object but can receive additional abilities based on the data type given. This means that as a programmer it is possible to have one class but have it work for all data types in a clear and concise manner. At the end of the day this means that a generic class will for certain return the type of data given to it even though we wrote it in a generic way.

Type aliasing is a close relative to the C language construct of a typedef. At the most basic level type aliasing (or typedef if you’re a C programmer) can allow you to redefine language types as your own. For example (in Hack): type Integer = int; Other examples popping into mind include a Vector designed to carry names and ID numbers (a vector of tuples) or simply defining a tuple as a point. Hack also provides a great example pertaining to the conversion of time using type aliasing. The example goes to show that combined with type annotations, type aliasing can create implicit error checking stronger than anything PHP has by default.

PHP is a web language and there are countless places where PHP programmers need to generate HTML for their websites. This without proper error checking can be disastrous from a security point of view. XHP is something that Facebook released before Hack and it allows for PHP developers to write UI code easily without worrying about properly escaping incoming variables. It also shifts the emphasis back towards creating the user interface rather than worrying about PHP syntax and introducing PHP errors and vulnerabilities. Hack provides XHP by default but it also allows for methods to specifically return XHP code. What’s more the type of XHP can be tailored further to say that a certain element will be returned (i.e. div, span, box).

Hack also provides strict mode. Strict mode is exactly what one thinks, Hack will catch every error and all code must be correctly typed. By default strict mode is not enabled and this is probably fine for most development environments. In the default mode code is only type checked if the methods themselves ask to be type checked by returning a specific type. With that being said there are times in which strict mode will be beneficial. When dealing with data where type is crucial to code execution strict mode makes sense. Strict mode also comes to mind for those just learning how to program. In the past I’ve been hesitant to recommend PHP as a first programming language because it was so forgiving. I prefer Java for that because it will tell the programmer when they are wrong or are doing something that is just not quite right. With strict mode for Hack there is room for exploration as to whether or not Hack is viable for teaching programming.

Hack is very exciting, it iterates PHP in such a way that brings PHP into the modern world of programming. The one question that did come up though is, why wasn’t this just brought into PHP as an add-on? It would certainly be feasible to add Hack type features on to PHP with add-ons but there’s something bigger at play here. Facebook itself has become a company that is known for publishing code quickly. Hack allows them to have the freedom of PHP but apply a rapid development model to the language itself. By staying separate from PHP and running on HHVM (a virtual machine) Hack can run faster with the benefits of HHVM and innovate faster than the core PHP language. For a company with a rapid development cycle this is a win-win and it also helps the programming language ecosystem by showing what is possible on the web today.