I am the admin at the category of Mobile Applications, Internet, Startups, Merchant and Operating Systems. Thank you

12377615-a-blue-binary-codes-background-Stock-Photo-binary-code-matrixYou’ve been thinking of building artificial intelligence to help you write codes? A more perfect codes, like building a robot to help you beat the best guy in an online chess game. Facebook has been using Hack Codegen to generate codes.

Facebook has open-sourced Hack codegen. Hack codegen is a library for generating codes by Facebook. Facebook simply call it “Writing codes that write codes”. Using Hack Codegen enables programmers to increase the level of abstraction by making frameworks that are declarative and that are translated into high-quality Hack code.

Before Hack codegen was developed, Facebook has been generating code through concatenating strings and a few helper functions, it was cool but not good enough for Facebook.  They see the need to improve their systems for writing and reading data, and that’s where the motivation came from.

Hack Codegen Library includes, hack_builder, which deals with the concatenation, new lines, indentation, braces, hack keywords, collections, and more.

An example  is:

hack_builder()

->startForeachLoop(‘$users’, ‘$id’, ‘$user’)

->startIfBlock(‘$id === $search’)

->addReturn(‘$id’)

->endIfBlock()

->endForeachLoop();

….and generates this code:

foreach ($users as $id => $user) {

if ($id === $search) {

return $id;

}

}

When the length is too long, the builder breaks the line automatically on some points. Other common construct is also is defined in the Hack codegen, they are classes, methods, variables, functions, traits, interfaces, and files

An example of  a way of defining a class with a method is :

codegen_class(‘HelloWorld’)

->addMethod(

codegen_method(‘sayHi’)

->setBody(‘echo “hello world\n”;’)

);

And it generates this code:

class HelloWorld {

public function sayHi() {

echo “hello world\n”;

}

}

On the announcement of the open-sourcing Hack Codegen that generates hack code automatically Facebook’s software engineer Alejandro Marcu said in a blog post:

“Hack Codegen is a library for generating Hack code and writing it into signed files that prevent undesired modifications. 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. 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.”

Signing files:

“We wanted to make sure that engineers didn’t edit signed files, so that we could regenerate code automatically when they change a schema. We could have added comments saying the code must not be changed, but we were afraid they might be overlooked. Instead, we used a library that was already developed at Facebook to sign files. It works by hashing the contents of the file and writing it on the header of the file. Then, it can verify whether the hash matches the contents to know if it was modified, and have tools in place to stop this — with a commit hook, for example.

 

The header of a signed file looks something like this:

 

/**

* This file is generated. Do not modify it manually!

* Run php ./scripts/generate_code.php to regenerate

*

* @generated SignedSource<<d6168d52d82d350d4907c1e835f6f2f5>>

*/

However, we wanted to offer more flexibility in some parts of the generated code. For example, a field may need to do post-processing in the getter. One way of solving that was to keep the files completely auto-generated but allow classes to be extended or provide hooks to insert custom code in another file. The other way was to allow for sections of the file to be manually written. We opted for the latter because we thought it would make code easier to read and write. We extended the file signature library to support this by removing the manual sections from the signature. Also, we updated the code generation so that it would keep the manual parts. “

The link to the open source is https://github.com/facebook/hack-codegen.

You can now write codes that write codes for you.

(Visited 158 times, 1 visits, Share this post for more visits)

Related Post

Come On, Will You Go Without Sharing This?

Leave a Reply

Your email address will not be published. Required fields are marked *