Hack – The Language Behind Facebook
Hack is the new language behind Facebook, which is still the most popular social network to date. It’s a web programming language invented and (recently) open-sourced by Facebook. The company claims that the language helps programmers to code programs faster and avoid errors early and easily.
(Image credit: guardianlv.com)
Hack is a language used to build complex websites at great speeds while still ensuring that the site’s source code is well organized and comparatively free of errors. Its "safety net" features allow programmers to write safer code which reduces the chances of later stepping into troubles.
In this post, we’ll look into Hack and some of its features which are arguably better than PHP, currently the most popular web programming language driving most websites. Hack is built to run on the Facebook’s HHVM virtual machine, which is known to deliver superior performance.
Recommended Reading: 10 Misconceptions That Are Not True About Learning Programming
The Dynamics Of A Problem
Back in 2003, when Mark Zuckerberg started building Facebook, he used a web development language called "PHP". It was the most popular and (relatively) easy programming language at the time to create dynamic websites, with great speed.
PHP is a dynamically-typed language, which means you need not spend time in defining variables and once you finish your code, you can almost run it instantly. This eases the coding and decreases the development time and effort, but heavily increases the chances of getting into errors, which only shows its ugly head at the time of execution.
Can’t Afford To Have Mistakes
This further intensifies the problem as you need to run the code in order to find errors (unlike statically-typed languages). Thee errors continue to grow with the growth of the codebase. Small projects may not face a big issue, but with a large codebase with 5 errors per thousand-line code potentially carrying up to 5000 errors – that’s a lot to debug.
The situation worsens with Cloud-scale companies like Facebook when thousands of programmers write and ship new code every day. They also can’t afford to have errors in their code, which may lead to user data being compromised. So what is there to do?
Reinventing The Wheel
Since Facebook’s front-end was mostly written in PHP, switching to a new language would mean having to migrate the whole site code, which is not just impractical but also not feasible. Plus, if the programmers are already used to PHP, this calls for a massive reboot in human resource.
Luckily there is a better solution – they re-invented a language, derived from PHP, which can co-exist with this traditional language.
"Thus, Hack was born. We believe that it offers the best of both dynamically typed and statically typed languages, and that it will be valuable to projects of all sizes," it was announced on Facebook’s Engineering blog.
Hack Is Simply Better PHP
Essentially, Hack is "better PHP". Derived from PHP, it inter-operates seamlessly with PHP for faster and safer web development. You can have a project containing PHP and Hack code side-by-side and still, the project runs as required. This is the key feature that is going to attract PHP developers to try Hack. And possibly encourage them to gradually migrate their PHP code to Hack.
Hack primarily adds to PHP the power of static typing along with many more features found in other modern programming languages. It’s a language developed for HHVM (HipHop Virtual Machine), an open-source runtime platform built by Facebook to execute programs written in Hack and PHP.
The Hack and HHVM combo is targeting one of the foundations of modern web: rapid app development. It’s never been easy to test and debug web applications, but Hack is changing that by allowing programmers to detect errors early on without compromising the development cycle of PHP. The conclusion from several tech sites has been that Hack is good news.
Advantages Of Hack
The greatest enhancement that Hack provides over PHP is the removal of unnecessary and error-prone features. Hack adds safety nets (without slowing you down) so you make less errors. It also adds various features found in modern programming languages which makes writing code in Hack enjoyable.
Hack is both a dynamically typed and statically typed web development language, thus bringing the best of both typed languages. This is actually called "gradual typing," a type system which allows variables to be typed either at compile-time or at run-time. It can run your code without compiling – you can edit a file and reload the webpage and see the changes instantly.
Hack brings in features such as collections, lambda expressions, and run-time enforcement of return types and parameter types, addition of generics, asynchronous programming, etc.
These new features are non-obstructive, thus the code written using Hack will still look and feel like the traditional dynamic code created using PHP. Moreover, engineers will better understand the code as static typing acts a lot like documentation.
What’s Not So Advantageous
The greatest disadvantage of Hack is that it has abandoned the features that make PHP a simpler language for beginners. You cannot embed the HTML directly in your source code, and you can’t have a code written outside of a function or class. But this is a relatively small problem. While it may deter beginners, PHP programmers would not find it hard to adapt to Hack and will probably deem the advantages of Hack convincing enough for adoption.
Facebook has already deployed Hack on its website, which serves more than 1.2 billion people in the world. "We have deployed Hack at Facebook and it has been a great success. Over the last year, we have migrated nearly our entire PHP codebase to Hack," stated the article on the Facebook Engineering blog.
It’s going to be interesting to see how the PHP community at large will adopt this new language. We hope to see Hack supported on other PHP virtual machines and parsers, which will simplify code migration.
An open source project also means it’s not dependent on its original creators for new features and bug corrections. We may encounter some awesome feature in the future suggested or added by the open source developers community to this new language.
Moreover, Facebook is also working to improve Hack. They did create the language after all. "This is just the first step, and we are dedicated to continuing to evolve this software to make development even easier for both our own engineers and the broader community," as posted on the Facebook Engineering blog.
What do you think about Hack? Do you think Hack is better than PHP? Can it replace PHP? Please post your answers through comments.
Author: Ashutosh KS
Ashutosh is a tech enthusiast who is interested in software technologies. He is a supporter of open source software. Apart from tech he loves the movies and reading novels.