When Facebook announced Hack, a programming language that makes over PHP, it elicited with some pretty excitable reactions, with some going as far as to pronounce it “the programming language of the future“.

Hack makes many changes to PHP, but does it go far enough to redeem the oft-maligned scripting language in the eyes of its many detractors?

Perhaps chief among PHP’s critics, at least in how often he is cited, is programmer Alex Munroe, whose article PHP: a fractal of bad design tops Google searches for “PHP bad”.

Does Munroe feel the changes that Facebook implemented in Hack significantly improve PHP?

The good

Munroe views the greatest improvement that Hack offers over PHP as the removal of unnecessary and error-prone features.

“By far most relevant to my list of grievances is the list of features Hack refused to inherit from PHP,” he said.

“All of them are either duplicates of other features (like ‘endif’), “convenience” features that mostly lead to bugs (like undefined variables), or easy conflation of data and code (like $$x). Good riddance to all of them.

“This is by far the best thing Hack does for PHP, but it’s gotten very little attention and is buried at the end of the documentation. Facebook should be shouting this list from the rooftops.”

In contrast, what generated the most attention when Hack was announced last week was the introduction of a type annotations, which allows types to be explicitly set for parameters, class member variables and return values.

Hack checks annotations in source files in the background. A system daemon monitors the filesystem, and when changes are saved to disk a type checker will quickly analyse the changes. Facebook indicates that this process takes an average of 200ms.

Munroe describes the approach Hack takes to type checking as running a Lint, a program which examines source code for potential errors before it is run.

“What strikes me most about it is that it’s apparently one big linter, not part of the runtime,” he said.

“Hack adds a lot of syntax, but none of the typing seems to affect how a program actually runs. It only dispenses advice on whether you should be running it in the first place.”

Another major element of Hack is support for asynchronous programming, which allows a script to juggle multiple tasks. This feature can be particularly useful where a block of code is waiting for a response from another server before it can continue, as it frees up the computer to execute other tasks in the interim.

Hack introduces the ‘async’ and ‘await’ keywords to define code that should be executed concurrently, similar to the approach taken in C#. The keywords provide a simple way of specifying an ‘async’ method whose execution will be suspended, that is the control flow will be returned to the caller of the ‘async’ method, until the designated ‘await’ method returns.

“Async is actually pretty interesting, as it’s billed as a cooperative system, akin to the asyncio module just released with Python 3.4,” said Munroe.

“The documentation is unfortunately sparse, with only a single example that doesn’t perform any actual blocking work. I think (skimming the source here) that ‘await’ really does cooperate, but ‘async’ rigs a function to be run in a thread.

“So there are the stubs of a cooperative system here, but in practice it can’t be a true nonblocking single-threaded system until there are some I/O APIs that hook into the scheduler or allow for doing so.

“Baking this into the language is also a tradeoff, since now it’s easy to add async code to existing sync code, but the developer has little control over or insight into the scheduler and what it’s doing.”

Other changes from PHP include the introduction of collections that provide first class, built-in paramatised types such as Vector, Map, Set and Pair; and of lambdas, which offer similar functionality to PHP closures, but capture variables from the enclosing function body implicitly.

Does Hack change enough?

In spite of the changes Hack makes to PHP, Munroe isn’t convinced it is enough to win him over.

“Would all this be enough for me to enjoy programming in Hack? Doubtful.

“The standard library is still, necessarily, the same massive list of global functions. There are still at least two radically different ways to do almost anything — and with Hack’s additions, there are now two ways to write a lambda and two ways to have a typed struct.

“Class and function names are still case-insensitive, and in fact Hack disallows a method ‘foo’ in a class ‘Foo’ because it looks like a constructor.

“The low-hanging fruit has been pruned, and that’s absolutely a great improvement, but I put “fractal” in the article title in the first place because the poor decisions permeate every corner of the language.”

Facebook has also designed Hack to not support features that make PHP a simpler language for beginners to get to grips with, he said.

“Hack has removed the way most beginners write PHP: you can’t have any code outside of a function or class, and you can’t embed HTML directly within your source code.

“So Hack loses the primary selling point of PHP, which is that it’s trivial to get “hello world” going and build up from there.”

Facebook made Hack to run on its HipHop Virtual Machine, the series of execution engines that Facebook uses to run its PHP and now Hack codebase. Facebook has designed Hack to make it relatively straightforward to convert existing PHP code, and provides conversion tools to assist with migration.

But in Munroe’s view Hack’s appeal is ultimately likely to be limited, both for existing PHP coders and new programmers.

“It’s unfortunate, but I don’t expect Hack to replace PHP, or even make a significant dent in it,” he said.

“Programmers who care deeply about static typing and strict semantics probably aren’t using PHP in the first place; new programmers just getting into web development aren’t going to bother with running a background linter; and the killer off-the-shelf apps like WordPress and MediaWiki/Wikipedia can’t port to Hack without breaking compatibility with the stock PHP everyone’s free host is running.

“It’s still a useful tool for massive existing closed-source PHP codebases that can’t easily be ported to another language, but there are only so many of those.

“I’m certainly glad to see PHP’s deficiencies brought to light in a positive way without the need for backlash, something I couldn’t do myself.

“But I’d still suggest that anyone starting a new project who’s interested in what Hack brings to the table just use a language that already has the features they like built in.”