Facebook Open-Sources Hack Codegen code
Facebook has decided to open-source a piece of software that is designed to write code. Facebook announced it is open-sourcing its Hack Codegen library for generating Hack code and writing it into signed files that prevent undesired modifications.
Hack Codegen is a library that allows users to generate Hack code, the company’s programming language developed for HipHop Virtual Machine. (HHVM is a virtual machine designed to execute programs in Hack and PHP.)
“Being able to generate code through automated code generation allows programmers to increase the level of abstraction by making frameworks that are declarative and that are translated into high-quality Hack code,” wrote Alejandro Marcu, software engineer at Facebook, in a blog post. “We’ve been using Hack Codegen at Facebook for a while. After seeing so much internal success, we open-sourced this library so that more people could take advantage of it.”
As said above, Hack is a programming language that Facebook developed for its HipHop Virtual Machine (HHVM) that interoperates with PHP programming language. Hack reconciles the fast development cycle of PHP with the discipline provided by static typing, while adding many features commonly found in other modern programming languages, Facebook engineers said.Hack provides instantaneous type checking by incrementally checking a developer’s files as they edit them. It typically runs in less than 200 milliseconds, making it easy for developers to integrate it into their development workflow without introducing a noticeable delay.
Before Hack Codegen, Facebook generated code through concatenating strings which it found that it wasnt enough to scale up.
“We realized early on that we would need a good library to generate code,” wrote Marcu. “At the time, we didn’t do that much code generation at [Facebook], mostly dumping values into arrays, so we didn’t have any good tools except for signing files. This is the need that motivated us to write this library.”
The lack of scaling facilities motivated the engineering team to work to complete the Hack Codegen project. Over the year and a half, Facebook migrated nearly its entire PHP codebase to Hack, thanks to both organic adoption and a number of homegrown refactoring tools.“After seeing so much internal use of Hack Codegen for diverse applications, it’s our pleasure to open-source this library for the external community to use,” Marcu said.
The technology can be found here.“It is interesting that we are getting back into automated code generation which was a big deal in a different era,” Hilwa said. “I think Codegen is a valuable new tool that can help developer productivity in the right settings. The issues typically for automated code generation have typically been learning the abstraction itself, and then the ability to accommodate changes in the generated code. Newer technologies are constantly innovating around these issues.”
The library includes hack_builder to deal with the concatenation, new lines, indentation, braces, hack keywords, and collections, as well as signed files to regenerate code automatically when a schema is changed.