What Is Facebook’s New Hack (HHVM) Programming Language?

hack

A year and a half ago Google was turning heads with its Google Go Programming language. While they kept the world distracted, another internet giant quietly stepped into the realm of computer programming too. Their language is called Hack, and you’ve probably heard of the company – Facebook.

While it took Google Go around 3-4 years to win over the programmers of the world, Facebook’s Hack has achieved the same feat in less than two. Although directly comparing them seems obvious, it would be unfair since there’s a gap of 4 – 5 years between their respective births. However since they both come from leading players of the web and claim to be the future of programming, it’s difficult to keep the comparison scales locked away.

Hack is a programming language designed for HHVM. HHVM is a virtual machine set up and designed to run Hack and PHP codes. It seems like a match made in heaven, apart from one small detail; when it launched, Hack was regarded by some cynics as a ‘poor man’s PHP’. Facebook’s response? Transforming 90% of its code from PHP to Hack. Considering they have over 1 billion users and 24/7 traffic, the language must have some substance.

I know what you’re thinking: ‘of course they changed their code. They didn’t make their own programming language for nothing!’ True, but let’s look at the bigger picture. Regardless of who designed it, Hack is a language that’s less than 2 years old. And it’s holding up the backend of a website with 1.5 billion active users. That’s some serious stuff.

Why should I learn Hack?

Once upon a time, C and C++ were the only coding languages anyone used. Then Java came along, and certain people said ‘we have C, C++. Why learn a new language that just does the same thing?’ You can probably guess what happened to those folks.

The world is in a constant state of evolution. In 5 – 10 years, there’s a high possibility that Hack could leave PHP in the dust and revolutionise backend programming of websites. So why wait? Get ahead of the curve and learn Hack now. It takes the best of PHP and other programming languages and moulds them into one vastly improved end-product. That may sound heavy, but look at it this way; a lot of the code already resembles PHP and other Object Oriented Programming – so you won’t have to start from scratch.

Take a look for yourself. Here’s the customary “Hello World” example you’ll no doubt have seen in an array of programming lessons.

<?hh
function test(): string {
      $str_01 = 'Hello World';
      return str_01;
}

Key points to note:

  1. The beginning of a Hack code is very similar to the beginning of a PHP code. The only difference is the change from ‘<?php’ to ‘<?hh’.
  2. Unlike PHP, Hack does not have an ending tag to mark the end of the Hack code structure.
  3. Note the difference in which the return type of a function is written alongside the definition of the function.
  4. Code indentation has been a key feature of programming languages of late and Hack has continued the trend.
  5. The variables in Hack are declared exactly the same way as they were declared in PHP, starting with a ‘$’ sign.

Here’s another example:

&amp;amp;lt;?hh
function test_return(string $x): string {
        return $x;
}
function test_input(): void {
       $str_01 = 'hello';
       test_return($str_01);
}

Key points to note:

  1. Notice that the data type of the variable passed as a parameter to the function test_return() is mentioned explicitly. It is important to note that Hack has introduced Type Annotations in the programming language and not adhering to them would throw errors in the code.

Wait, what? Why should I explicitly mention that it’s a string? Can’t Hack judge that by itself? Has Hack lost the loosely type nature that PHP variables had?

Well, Hack has certainly not lost the loosely type nature that PHP variables had. In fact you can notice that the variable $str_01 does not require any data type to be mentioned with it. So why do we need to give data types with function parameters? The answer to that is another concept called ‘Type Annotations’ that Hack has included in its armoury. Type Annotations will be discussed in detail in the next blog when we describe the key features of Hack. But give you a taste; here is a quick reference to what might have happened if we did not use it.
Imagine we are taking the value in a variable $my_num and passing it to a function ‘calc()’. The condition inside the function ‘calc()’ says that while the parameter passed to the function is greater than 0 then print True else print False. All good, right?
But we’re taking it from the user. So what if the user passes a non-integer value, a string value, to the variable ‘$my_num’? The loosely type nature of variables would mean that it will be accepted and the same would be passed to the function ‘calc()’. In short, the whole code would run without giving an accurate result. To avoid such scenarios, Hack has introduced Type Annotations as part of its coding practice. But again, that’s a story for the next blog.

(Visited 1,215 times, 1 visits today)